【问题标题】:combine two selectbox values in r shiny在 r shiny 中组合两个选择框值
【发布时间】:2019-04-27 21:05:48
【问题描述】:

我需要在 R Shiny 中组合两个选择框值。 选择框 1 有年份,选择框 2 有月份。

如果用户选择 2018 和 06,我应该将 2018-06 放入变量中。

我尝试了paste(input$year,input$month,sep="-") 但它不起作用。

【问题讨论】:

    标签: r select shiny concatenation selectinput


    【解决方案1】:

    这应该可以,请注意我从reative 更改为reactiveValues,因为我认为这对您来说应该更直观,您可以使用v$value,它将包含您想要的内容。我建议您阅读https://shiny.rstudio.com/articles/reactivity-overview.html,以便更好地了解正在发生的事情

    library(shiny)
    
    ui <- fluidPage(
      textOutput("value"),
      selectInput("year","year",choices = c(2017,2018),selected = 1),
      selectInput("month","month",choices = c(1:12),selected = 1)
    
    )
    
    server <- function( session,input, output) {
    
      v <- reactiveValues(value=NULL)
    
      observe({
        year <- input$year
        month <- input$month
        if(nchar(month)==1){
          month <- paste0("0",month)
        }
        v$value <- paste(year,month,sep="-")
      })
    
      output$value <- renderText({
        v$value
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 如何将此值放入某个变量中?
    • 该值在名为Combined()的变量中
    • Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
    • 如果您查看 ?reactive ,您会发现它是一个表达式,不能用作值,您可以使用 reactiveValreactivevalues
    • 我仍然遇到同样的错误,我闪亮的应用程序正在关闭
    猜你喜欢
    • 1970-01-01
    • 2017-06-11
    • 2011-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-02-24
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多