Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用observeEvent导致内存泄漏_R_Memory_Memory Leaks_Shiny_Observers - Fatal编程技术网

使用observeEvent导致内存泄漏

使用observeEvent导致内存泄漏,r,memory,memory-leaks,shiny,observers,R,Memory,Memory Leaks,Shiny,Observers,我使用以下代码获得累积内存。每次我在操作按钮1和2之间切换时,使用的内存都会增加 library(ggplot2) library(shiny) library(lobstr) ui <- navbarPage("Test",fluidPage(fluidRow( column(width = 1, actionButton("action_input_1", label = "1")), column

我使用以下代码获得累积内存。每次我在操作按钮1和2之间切换时,使用的内存都会增加

library(ggplot2)
library(shiny)
library(lobstr)

ui <- navbarPage("Test",fluidPage(fluidRow(
                     column(width = 1, actionButton("action_input_1", label = "1")), 
                     column(width = 1, actionButton("action_input_2", label = "2")),
                     column(width = 10, plotOutput("plot", width = 1400, height = 800)))))

server <- function(input, output) {
  # 1
  observeEvent(input$action_input_1, {
    output$plot <- renderPlot({
      plot(rnorm(100))
    })
    print(cat(paste0("mem used 1: ", capture.output(print(mem_used())),"\n")))
  })

  # 2
  observeEvent(input$action_input_2, {
    output$plot <- renderPlot({
      plot(rnorm(1000))
    })
    print(cat(paste0("mem used 2: ", capture.output(print(mem_used())),"\n")))
  })
}
shinyApp(ui, server)
库(ggplot2)
图书馆(闪亮)
图书馆(lobstr)

ui使用reactiveVal如何:

reactiveData <- reactiveVal(NULL)
observeEvent(input$action_input_1, reactiveData(rnorm(100)))
observeEvent(input$action_input_2, reactiveData(rnorm(1000)))
output$plot <- renderPlot(plot(reactiveData()))

使用reactiveVal如何:

reactiveData <- reactiveVal(NULL)
observeEvent(input$action_input_1, reactiveData(rnorm(100)))
observeEvent(input$action_input_2, reactiveData(rnorm(1000)))
output$plot <- renderPlot(plot(reactiveData()))

请注意,这使用了
observeEvent
,但没有嵌套
renderPlot
,我认为这是导致内存泄漏的原因。谢谢,这在示例中效果很好,但在我的用例中不起作用。实际上,我想在第一个实例中将多个参数传递给reactiveData。我想我必须使用反应值,但它不起作用。请检查此代码:
reactiveData您已关闭。更新了我上面的答案。太好了,它起作用了。你已经回答了我的问题。但我遇到了另一个问题:如果我使用“observeEvent”而不是“observeEvent”(因为reactiveData会从文件自动更新),在渲染新绘图时,我仍然会增加内存。我还尝试了
rm(list=ls())
gc(reset=TRUE)
gc()
但它似乎没有任何效果。请注意,这使用了
observeEvent
,但没有嵌套
渲染图
,我认为这是导致内存泄漏的原因。谢谢,这在示例中效果很好,但在我的用例中不起作用。实际上,我想在第一个实例中将多个参数传递给reactiveData。我想我必须使用反应值,但它不起作用。请检查此代码:
reactiveData您已关闭。更新了我上面的答案。太好了,它起作用了。你已经回答了我的问题。但我遇到了另一个问题:如果我使用“observeEvent”而不是“observeEvent”(因为reactiveData会从文件自动更新),我在绘制新绘图时仍会增加内存。我还尝试了
rm(list=ls())
gc(reset=TRUE)
gc()
,但似乎没有效果。
reactiveData <- reactiveValues(rnorm = NULL, bool_val = NULL) 

observeEvent(input$action_input_1,  {# reactiveData(rnorm(100), bool_val <- TRUE)) 
   reactiveData$rnorm <- rnorm(100)
   reactiveData$bool_val <- TRUE
})

observeEvent(input$action_input_2, { #reactiveData(rnorm(1000), bool_val <- FALSE)) 
  reactiveData$rnorm <- rnorm(1000)
  reactiveData$bool_val <-  FALSE
})

output$plot <- renderPlot(plot(reactiveData$rnorm))
reactiveData <- reactiveVal(list(rnorm = NULL, bool_val = NULL)) 

observeEvent(input$action_input_1, reactiveData(list(rnorm = 100, bool_val = TRUE)))

observeEvent(input$action_input_2, reactiveData(list(rnorm = 1000, bool_val = FALSE)))

output$plot <- renderPlot(plot(reactiveData()$rnorm))