从收音机按钮输入的闪亮开关导致关于反应上下文的错误

时间:2017-01-18 19:15:03

标签: shiny

我正在尝试访问渲染函数外部闪亮的单选按钮的值,但它总是返回有关反应上下文的错误。为什么我需要有一个反应式上下文来访问它的值,我不应该在闪亮的服务器函数中看到它吗?

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

  # Application title
  titlePanel("Old Faithful Geyser Data"),

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      radioButtons("dist", "Distribution type:",
                   c("Normal" = "norm",
                     "Uniform" = "unif",
                     "Log-normal" = "lnorm",
                     "Exponential" = "exp"))
    ),
    mainPanel(
      plotOutput("distPlot"),
      textOutput('dirtText')
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  # This causes the reactive error
  d <- switch(input$dist,
           norm = {print('duck1')},
           unif = {print('duck2')},
           lnorm = {print('duck3')},
           exp = {print('duck4')},
           {print('goose')})
  }
  # should now be a function pointer to one of the code blocks.

  output$distPlot <- renderPlot({
    # Yet this does not
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)

    print(input$dist)

    hist(dist(500))
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

反应误差如下。

Listening on http://127.0.0.1:5181
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):
    46: .getReactiveEnvironment()$currentContext
    45: .subset2(x, "impl")$get
    44: $.reactivevalues
    43: $ [/home/hschmale/wildfig/new_dashboard/switch_reactive_not_working/app.R#37]
    42: server [/home/hschmale/wildfig/new_dashboard/switch_reactive_not_working/app.R#37]
     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.)

1 个答案:

答案 0 :(得分:1)

如错误消息中所指定,您需要将switch语句包含在被动反应中,例如:

d <- reactive(switch(input$dist,
       norm = {print('duck1')},
       unif = {print('duck2')},
       lnorm = {print('duck3')},
       exp = {print('duck4')},
       {print('goose')}))

这是因为当您第一次运行input$dist函数时未定义server,它仅在renderXXX或任何其他reactive函数中定义

修改

但为了实现这一目标,您需要在某处使用d(),例如您在renderPlot中调用它:

server <- function(input, output) {
  d <- reactive(switch(input$dist,
                       norm = rnorm,
                       unif = runif,
                       lnorm = rlnorm,
                       exp = rexp,
                       rnorm))
  output$distPlot <- renderPlot({hist(d()(500))})
}