调整输入变量并在Shiny

时间:2016-08-25 18:58:50

标签: r shiny

ui.R

library(shiny)

shinyUI(fluidPage(
  checkboxGroupInput("Check1",label=h4 ("Coin Type:"), choices = c("Fair","Unfair (p = 60% for heads)"))
))

server.R

library(shiny)

shinyServer(function(input, output) {
  if (input$Check1 == "Fair"){
    x <- 1
    output$Value <- renderTable(as.data.frame(x))
  } else { 
    x <- 5
    output$Value <- renderTable(as.data.frame(x))
  }
})

这应该很简单:应该有两个复选框。当&#34; Fair&#34;选中,给x值1;否则,5。然后将其显示在数据框上。

我认为问题在于分配变量x。这是我得到的错误:

Warning: Error in .getReactiveEnvironment()$currentContext: 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.)
Stack trace (innermost first):
    44: .getReactiveEnvironment()$currentContext
    43: .subset2(x, "impl")$get
    42: $.reactivevalues
    41: $ [-- this has a folder directory --]
    40: server [-- this has a folder directory --]
     1: runApp
Error in .getReactiveEnvironment()$currentContext() : 
  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.)

[请注意,我省略了上面的4140]。

我该如何处理这个问题?搜索让我一无所获,我不清楚如何使用reactiveValues

1 个答案:

答案 0 :(得分:1)

你需要让x反应来做你想做的事情:

x <- reactive({
  if (input$Check1 == "Fair"){
  1
} else { 
  5  
}
# pass reactive x() to renderTable
output$Value <- renderTable(as.data.frame(x()))