ggvis plots rendered using bind_shiny() aren't reactive

荒凉一梦 提交于 2019-12-07 12:59:45

问题


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


来源:https://stackoverflow.com/questions/28081631/ggvis-plots-rendered-using-bind-shiny-arent-reactive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!