简体   繁体   中英

ggvis plots rendered using bind_shiny() aren't reactive

I think I'm missing some basic aspect of ggvis + shiny.

Following the tutorials, plots are constructed in server.R using a series of %>% pipes, ending with bind_shiny, which associates the plot with an identifier that can be referred to in ui.R

What I don't get though is that the plot itself is not reactive in the way that code within renderTable(), renderText() or reactive() will be. So if I want to refer to an input parameter like input$x in defining the plot, it won't work, I'll get an error saying "Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)" .

For example, if 'input' is the input parameter to the shinyServer function, I might have a plot that looks like:

dataframe %>% ggvis(~ aval, ~ bval) %>% layer_points %>% layer_paths(x = ~xv, y = ~ yv, data = data.frame(xv = c(0, 0), yv = c(0, input$maxValParam)))

where layeR_points is used to plot data in dataframe and layer_paths is being used to draw a vertical line up to the maxValParam value.

So this answer might be useful.

It looks like in order to reference your input$maxValParam inside a ggvis() function, the entire ggvis function needs to be wrapped in a reactive. blatantly ripping off the above answer, yours might look something like:

reactive({
  dataframe %>% 
      ggvis() #rest of plotting code %>% 
      add_axis("x", title = input$maxValParam)
}) %>% bind_shiny("plot")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM