【问题标题】:Shiny Tutorial Error in RR中的闪亮教程错误
【发布时间】:2021-01-09 05:55:32
【问题描述】:

我看到了一些很酷的用 R 语言来制作 web 应用程序的闪亮用法,并想尝试自己学习如何使用它。我现在正在做教程,但是当我进入教程的输入和输出部分时 (http://rstudio.github.io/shiny/tutorial/#inputs-and-outputs) 我遇到了一个问题。

具体来说,我收到一条错误消息:

.getReactiveEnvironment()$currentContext() 中的错误: 如果没有活动的反应上下文,则不允许操作。 (你试图做一些只能在反应函数内部完成的事情。)

我尝试了很多不同的方法并在网上到处搜索,但无法找出问题所在。我在 OS X 版本 10.8.3 上运行 R 版本 2.15.2。我的默认浏览器是 Chrome。

感谢您的帮助。

【问题讨论】:

标签: r shiny


【解决方案1】:

我知道这个问题有点过时了,但对那些在遇到相同错误消息时可能会来搜索的人作出回应。

由于您没有包含您的代码,让我们看看为什么会出现此错误消息。

当错误消息显示“Operation not allowed without an active reactive context.”时,它的意思是您正在访问ShinyServer 函数内的“反应性”元素,但是外部任何反应函数,例如renderTablerenderPlot()等。

这在 ShinyServer() 中不起作用

shinyServer(function(input, output) {
    abc <- input$some.input.option   

  #other reactives here

})

修复:将其包裹在 reactive

这将起作用:

shinyServer(function(input, output) {
  abc <- reactive({
   abc <- input$some.input.option    
  })

  #other reactives here

})

现在,在 ShinyServer 函数内部,您可以通过调用 abc() 访问该 Input 参数 注意括号,因为它是一个反应函数。

希望对您有所帮助。

【讨论】:

  • 谢谢,这有帮助,但你能定义“反应元素”吗?
  • 您能否详细说明为什么您重复使用名称abc
  • @MichaelChirico(以及其他想知道的人):为了清楚起见,重复了 abc 这个名字。反应对象abc() 可在整个服务器函数中访问,包含与reactive({}) 函数中的对象abc 相同的数据。给两个相同的名称可以立即清晰地显示出来。
【解决方案2】:

对我来说,当我忘记使用renderPrint 时,我遇到了这个问题,当你刚启动时很容易忘记。

例如:

shinyServer(function(input,output) {
  output$outputString <- input$something
  }
)

当我真正需要做的是

shinyServer(function(input,output) {
  output$outputString <- renderPrint({input$something})
  }
)

【讨论】:

  • 恭喜,但如果某物是数值,还有什么比 as.numeric(renderPrint({input$something})) 更好的吗?
猜你喜欢
  • 1970-01-01
  • 2017-03-24
  • 2015-12-02
  • 2019-08-15
  • 2015-07-02
  • 2013-05-16
  • 2021-07-17
  • 1970-01-01
相关资源
最近更新 更多