ggvis icon indicating copy to clipboard operation
ggvis copied to clipboard

Should make sure that bind_shiny doesn't leave observers floating around

Open wch opened this issue 11 years ago • 3 comments

bind_shiny can potentially leave observers floating around each time the user selects a new value:

server <- function(input, output) {
  observe({
    xvar <- if (is.null(input$column)) "wt" else input$column
    xprop <- prop("x", as.name(xvar))
    mtcars %>% ggvis(xprop, ~mpg) %>% layer_points() %>% bind_shiny("plot1")
  })
}

ui <- fluidPage(
  selectInput("column", "Column for x", c("wt", "mpg", "disp", "hp")),
  ggvisOutput("plot1")
)

shinyApp(server = server, ui = ui)

On the other hand, this would not, because bind_shiny is run just once:


server <- function(input, output) {
  reactive({
    xvar <- if (is.null(input$column)) "wt" else input$column
    xprop <- prop("x", as.name(xvar))
    mtcars %>% ggvis(xprop, ~mpg) %>% layer_points() 
  }) %>% bind_shiny("plot1")
}

ui <- fluidPage(
  selectInput("column", "Column for x", c("wt", "mpg", "disp", "hp")),
  ggvisOutput("plot1")
)

shinyApp(server = server, ui = ui)

We should make sure that either (A) bind_shiny is run only outside of a reactive, or (B) when bind_shiny is run with a name that has already been bound, all the previously-created observers are suspended.

This is related to #165.

wch avatar Sep 09 '14 18:09 wch

Can't we make observers suspend themselves on gc? Or do they need to be suspect before they get collected?

On Tuesday, September 9, 2014, Winston Chang [email protected] wrote:

bind_shiny can potentially leave observers floating around each time the user selects a new value:

server <- function(input, output) { observe({ xvar <- if (is.null(input$column)) "wt" else input$column xprop <- prop("x", as.name(xvar)) mtcars %>% ggvis(xprop, ~mpg) %>% layer_points() %>% bind_shiny("plot1") })}

ui <- fluidPage( selectInput("column", "Column for x", c("wt", "mpg", "disp", "hp")), ggvisOutput("plot1"))

shinyApp(server = server, ui = ui)

On the other hand, this would not, because bind_shiny is run just once:

server <- function(input, output) { reactive({ xvar <- if (is.null(input$column)) "wt" else input$column xprop <- prop("x", as.name(xvar)) mtcars %>% ggvis(xprop, ~mpg) %>% layer_points() }) %>% bind_shiny("plot1")}

ui <- fluidPage( selectInput("column", "Column for x", c("wt", "mpg", "disp", "hp")), ggvisOutput("plot1"))

shinyApp(server = server, ui = ui)

We should make sure that either (A) bind_shiny is run only outside of a reactive, or (B) when bind_shiny is run with a name that has already been bound, all the previously-created observers are suspended.

— Reply to this email directly or view it on GitHub https://github.com/rstudio/ggvis/issues/254.

http://had.co.nz/

hadley avatar Sep 09 '14 20:09 hadley

Observers need to be suspended before they're gc'ed. So we would need a way to suspend them when another bind_shiny is called with the same name.

wch avatar Sep 09 '14 21:09 wch

Is there any work around that could be used in the meantime?

ttzhou avatar Jun 26 '18 20:06 ttzhou