情境:我想将R闪亮UI中的输入变量打印到控制台。这是我的代码:
library(shiny)
ui=fluidPage(
selectInput('selecter', "Choose ONE Item", choices = c("small","big"))
)
server=function(input,output,session){
print("You have chosen:")
print(input$selecter)
}
shinyApp(ui, server)
不幸的是我收到此错误消息:
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.)
问题:我需要将代码更改为什么才能使其正常工作?
答案 0 :(得分:4)
您应该使用observeEvent
,每次输入更改时都会执行:
library("shiny")
ui <- fluidPage(
selectInput('selecter', "Choose ONE Item", choices = c("small","big")),
verbatimTextOutput("value")
)
server <- function(input, output, session){
observeEvent(input$selecter, {
print(paste0("You have chosen: ", input$selecter))
})
}
shinyApp(ui, server)