Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

shiny

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?

like image 746
nexonvantec Avatar asked Jan 16 '18 15:01

nexonvantec


1 Answers

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

like image 89
mlegge Avatar answered Sep 20 '22 10:09

mlegge