ggvis plots rendered using bind_shiny() aren't

2019-05-10 09:52发布

问题:

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.

回答1:

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")