繁体   English   中英

使用R Shiny中的selectInput()显示所有选项值

[英]Display all the option values using selectInput() in R Shiny

在我的例子中,选项是状态,而不是一组复选框,我有一个selectInput下拉列表。 我通过UI中的单个复选框触发逻辑。现在,我想要的是,每次单击该框时,我都应该在下拉列表中预先选择所有状态,并且需要用户输入未单击复选框的情况。 但遗憾的是,无论用户是否单击了复选框,输出始终是用户在下拉列表中选择的内容,即默认的“所有状态”不会作为预选择填充。

Server.R -  
    observe({
    if(input$national>0)
    {if (input$national %% 2 == 0){
        updateSelectInput(session,
                        "State",label = h4("Select any state"),                               
                        choices = list("NSW" = "NSW","Victoria" = "Victoria","SA" = "SA","Tasmania" = "Tasmania"),                                                                                                                                 
                        selected = c("NSW","Victoria","SA","Tasmania"),multiple = TRUE                                                          
                        )}
      else 
      {updateSelectInput(session,
                        "State",                                
                        label = h4("Select any state"),                               
                        choices = list("NSW" = "NSW","Victoria" = "Victoria","SA" = "SA","Tasmania" = "Tasmania"),                                                                                                                                 
                        selected = c(),multiple = TRUE                                                          
                          ) 
  }}
}) 

任何帮助都非常感谢,并提前多多感谢。

在你的ui.r中添加一个按钮

actionButton("selectall", label="Select/Deselect all")

在你的server.r让selectall修改你的选择器输入(这里我称之为show_vars )。

 observe({
  if (input$selectall > 0) {
    if (input$selectall %% 2 == 0){
      updateCheckboxGroupInput(session=session, 
                               inputId="show_vars",
                               choices = list("carat" = "carat",
                                              "cut" = "cut",
                                              "color" = "color",
                                              "clarity"= "clarity",
                                              "depth" = "depth",
                                              "table" = "table",
                                              "price" = "price",
                                              "x" = "x",
                                              "y" = "y",
                                              "z" = "z"),
                               selected = c(names(diamonds)))

    } else {
      updateCheckboxGroupInput(session=session, 
                               inputId="show_vars",
                               choices = list("carat" = "carat",
                                              "cut" = "cut",
                                              "color" = "color",
                                              "clarity"= "clarity",
                                              "depth" = "depth",
                                              "table" = "table",
                                              "price" = "price",
                                              "x" = "x",
                                              "y" = "y",
                                              "z" = "z"),
                               selected = c())

    }}
})

mod 2( %% 2 )使其每秒钟点击一次以选择全部,否则默认为第二个分支,您可以在其中预选任何您想要的内容(或者在我的情况下没有任何内容)。

暂无
暂无

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

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