简体   繁体   English

将selectInput存储在字符串/字符对象中

[英]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. 我想将输入$ var中的变量存储到一个对象,该对象可以与后者的字符串进行比较。 For now, I just tried to print it on the screen by storing it to an object value_stored . 现在,我只是试图通过将其存储到对象value_stored中在屏幕上打印。 But it not printing anything*(Error: cannot coerce type 'closure' to vector of type 'character'*). 但是它不打印任何内容*(错误:无法将类型'closure'强制转换为类型'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 . 我强烈建议使用reactiveValues (对象来存储你的反应值)和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. PS:您可以删除paste因为它在那里没有任何作用。

You are almost there, you just need to define the reactive element first and the watch the input inside an observeEvent() function. 差不多了,您只需要先定义反应性元素,然后在watchEvent()函数中监视输入即可。 Then inside the observe you use isolate() to update the value. 然后,在观察值内部,使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM