Situation: I want to print an input variable from the R shiny UI to the console. Here is my code:
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?
You should use an observeEvent
which will execute every time the input changes:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With