nexonvantec
nexonvantec

Reputation: 592

How do I print an input from the R shiny UI to the console?

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?

Upvotes: 9

Views: 14267

Answers (1)

mlegge
mlegge

Reputation: 6913

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)

enter image description here

Upvotes: 15

Related Questions