r - R Shiny - 将用户输入作为全局字符串传递
问题描述
如何接受用户输入并将其存储为 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)
解决方案
找到了解决方案。关键是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)