简体   繁体   中英

Store selectInput in a string/character object

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)

I would strongly suggest using reactiveValues (object to store your reactive values) and observeEvent .

Use server code:

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: you can remove paste as it does nothing there.

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.

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)

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