I want to store the variable in input$var to an object which can be compared to a string on the latter part. For now, I just tried to print it on the screen by storing it to an object value_stored. But it not printing anything*(Error: cannot coerce type 'closure' to vector of type 'character'*). That means it's not storing the value.
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)
You are almost there, you just need to define the reactive element first and the watch the input inside an observeEvent() function. Then inside the observe you use isolate() to update the value.
I would strongly suggest using
reactiveValues
(object to store your reactive values) andobserveEvent
.Use server code:
PS: you can remove
paste
as it does nothing there.