2015-06-19 158 views
3

在我的情况下,选项是状态,而不是一组复选框,我有一个selectInput下拉菜单。我通过UI中的单个复选框来触发逻辑。现在,我想要的是,每次单击该框时,应该在下拉列表中预先选择所有状态,并且需要用户输入复选框未被点击的情况。 但不幸的是,无论用户是否点击了复选框,输出始终是用户在下拉列表中选择的内容,即默认的“所有状态”未被填充为预选。使用R中的selectInput()显示所有选项值Shiny

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               
         ) 
    }} 
}) 

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

回答

2

在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()) 

    }} 
}) 

国防部2(%% 2)使得它的工作每秒点击全选,否则默认为第二支在那里你可以预先选择任何你想要的(或在我的情况没有)。

+0

我很久以前从另一个答案中拿出了这个,但是现在找不到它。 –

+0

非常感谢您的帮助Serban。我取得了一些进展,但需要更多的帮助。请看看帖子编辑。 –

相关问题