使用 bind_shiny() 渲染的 ggvis 图不是反应性的 [英] ggvis plots rendered using bind_shiny() aren't reactive

查看:56
本文介绍了使用 bind_shiny() 渲染的 ggvis 图不是反应性的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我错过了 ggvis + Shiny 的一些基本方面.

按照教程,在 server.R 中使用一系列 %>% 管道构建绘图,以 bind_shiny 结尾,它将绘图与可在 ui.R 中引用的标识符相关联

我不明白的是,情节本身并不像 renderTable()、renderText() 或reactive() 中的代码那样具有反应性.因此,如果我想在定义绘图时引用 input$x 之类的输入参数,它将不起作用,我会收到一条错误消息,说 "Operation not allowed without an active react context.(您尝试这样做一些只能从反应式表达式或观察者内部完成的事情.)".

例如,如果 'input' 是 ShinyServer 函数的输入参数,我可能有一个如下所示的图:

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

其中 layereR_points 用于绘制数据帧中的数据,而 layer_paths 用于绘制一条垂直线直至 maxValParam 值.

解决方案

所以 这个答案 可能有用.>

看起来为了在 ggvis() 函数中引用您的 input$maxValParam,整个 ggvis 函数需要包装在一个反应​​式中.公然撕掉上面的答案,你的可能看起来像:

反应式({数据帧%>%ggvis() #其余的绘图代码 %>%add_axis("x", title = input$maxValParam)}) %>% bind_shiny("plot")

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

这篇关于使用 bind_shiny() 渲染的 ggvis 图不是反应性的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆