将 selectInput 存储在 string/character 对象中
Store selectInput in a string/character object
我想将 input$var 中的变量存储到一个对象中,该对象可以与后面的字符串进行比较。现在,我只是尝试通过将它存储到一个对象 value_stored 来在屏幕上打印它。但它没有打印任何东西*(错误:无法将类型 'closure' 强制转换为类型 'character'* 的向量)。这意味着它没有存储值。
library(shiny)
ui <- fluidPage(
titlePanel("censusVis"),
sidebarLayout(
sidebarPanel(
helpText("Create demographic maps with
information from the 2010 US Census."),
selectInput("var",
label = "Choose a variable to display",
choices = c("Percent White",
"Percent Black",
"Percent Hispanic",
"Percent Asian"),
selected = "Percent White")
),
mainPanel(
textOutput("selected_var")
textOutput("test")
)
)
)
server <- function(input, output) {
output$selected_var <- renderText({
paste(input$var)
})
value_store <- reactive(input$var)
output$test <- renderText({
paste(value_store)
})
# I want to use the value in input$var for some comparision.
# but value_store unable to store. Help.
}
shinyApp(ui = ui, server = server)
我强烈建议使用 reactiveValues
(对象来存储您的反应值)和 observeEvent
。
使用服务器代码:
server <- function(input, output) {
# Create object for reactive values
rv <- reactiveValues(
value_store = character()
)
# When input changes -> update
observeEvent(input$var, {
output$selected_var <- renderText({
paste(input$var)
})
rv$value_store <- input$var
output$test <- renderText({
paste(rv$value_store)
})
})
}
PS:您可以删除 paste
,因为它在那里什么都不做。
你就快完成了,你只需要先定义反应元素,然后在 observeEvent() 函数中观察输入。然后在观察中使用 isolate() 来更新值。
ui <- fluidPage(
titlePanel("censusVis"),
sidebarLayout(
sidebarPanel(
helpText("Create demographic maps with
information from the 2010 US Census."),
selectInput("var",
label = "Choose a variable to display",
choices = c("Percent White",
"Percent Black",
"Percent Hispanic",
"Percent Asian"),
selected = "Percent White")
),
mainPanel(
textOutput("selected_var")
textOutput("test")
)
)
)
server <- function(input, output) {
value_store <- reactive(val = '')
observeEvent(input$var, {
# add to reactive
isolate(value_store$val = input$var)
# or render update
output$selected_var <- renderText({
paste(value_store$val)
})
})
output$test <- renderText({
paste(value_store$val)
})
# I want to use the value in input$var for some comparision.
# but value_store unable to store. Help.
}
shinyApp(ui = ui, server = server)
我想将 input$var 中的变量存储到一个对象中,该对象可以与后面的字符串进行比较。现在,我只是尝试通过将它存储到一个对象 value_stored 来在屏幕上打印它。但它没有打印任何东西*(错误:无法将类型 'closure' 强制转换为类型 'character'* 的向量)。这意味着它没有存储值。
library(shiny)
ui <- fluidPage(
titlePanel("censusVis"),
sidebarLayout(
sidebarPanel(
helpText("Create demographic maps with
information from the 2010 US Census."),
selectInput("var",
label = "Choose a variable to display",
choices = c("Percent White",
"Percent Black",
"Percent Hispanic",
"Percent Asian"),
selected = "Percent White")
),
mainPanel(
textOutput("selected_var")
textOutput("test")
)
)
)
server <- function(input, output) {
output$selected_var <- renderText({
paste(input$var)
})
value_store <- reactive(input$var)
output$test <- renderText({
paste(value_store)
})
# I want to use the value in input$var for some comparision.
# but value_store unable to store. Help.
}
shinyApp(ui = ui, server = server)
我强烈建议使用 reactiveValues
(对象来存储您的反应值)和 observeEvent
。
使用服务器代码:
server <- function(input, output) {
# Create object for reactive values
rv <- reactiveValues(
value_store = character()
)
# When input changes -> update
observeEvent(input$var, {
output$selected_var <- renderText({
paste(input$var)
})
rv$value_store <- input$var
output$test <- renderText({
paste(rv$value_store)
})
})
}
PS:您可以删除 paste
,因为它在那里什么都不做。
你就快完成了,你只需要先定义反应元素,然后在 observeEvent() 函数中观察输入。然后在观察中使用 isolate() 来更新值。
ui <- fluidPage(
titlePanel("censusVis"),
sidebarLayout(
sidebarPanel(
helpText("Create demographic maps with
information from the 2010 US Census."),
selectInput("var",
label = "Choose a variable to display",
choices = c("Percent White",
"Percent Black",
"Percent Hispanic",
"Percent Asian"),
selected = "Percent White")
),
mainPanel(
textOutput("selected_var")
textOutput("test")
)
)
)
server <- function(input, output) {
value_store <- reactive(val = '')
observeEvent(input$var, {
# add to reactive
isolate(value_store$val = input$var)
# or render update
output$selected_var <- renderText({
paste(value_store$val)
})
})
output$test <- renderText({
paste(value_store$val)
})
# I want to use the value in input$var for some comparision.
# but value_store unable to store. Help.
}
shinyApp(ui = ui, server = server)