Situation: I want to print an input variable from the R shiny UI to the console. Here is my code:
情况:我想将一个输入变量从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)
Unfortunately I get this error message:
不幸的是我收到此错误消息:
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.)
Question: What do I need to change my code to, in order to make it work?
问题:我需要将代码更改为什么才能使其正常工作?
1 个解决方案
#1
2
You should use an observeEvent
which will execute every time the input changes:
您应该使用一个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)
#1
2
You should use an observeEvent
which will execute every time the input changes:
您应该使用一个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)