简体   繁体   中英

R Shiny - Passing User Input as Global String

How do I take in a user input and store as environment string in server.R?

Here's an example (which produces an error):

library(shiny)

# Define the UI

n <- 100

ui <- bootstrapPage(
  numericInput('n', 'Number of obs', n),
  textOutput('count_new')
)

# Define the server code
server <- function(input, output) {

  count <- as.numeric(renderText({input$n}))
  output$count_new <- renderText({count/10})

}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

Found the solution. Key is to use reactive in front of input. The variable can then be called followed by () .

library(shiny)

# Define the UI

n <- 100

ui <- bootstrapPage(
  numericInput('n', 'Number of obs', n),
  textOutput('count_new')
)

# Define the server code
server <- function(input, output) {

  count <- reactive({input$n})
  output$count_new <- renderText({count()/10})

}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM