R Shiny-将用户输入作为全局字符串传递

时间:2018-11-10 17:40:31

标签: r shiny render reactive

如何接收用户输入并将其作为环境字符串存储在server.R中?

这是一个示例(会产生错误):

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)

1 个答案:

答案 0 :(得分:0)

找到了解决方案。关键是在输入之前使用reactive。然后可以调用该变量,后跟()

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)