为 shiny 开发 Select/Deselect All Button

Develop Select/Deselect All Button for shiny

受此启发 solution 我只是想知道是否可以在取消选中所有列名称后返回到按钮 "All"。当然我可以简单地开发else if函数:

if(input$radio == "All"){
  hw
} 
else if (length(input$show_vars) == 0){
  hw
}
else {
  hw[,input$show_vars,drop=FALSE]
}

并且结果计算 diamonds 数据集中的所有列,但在 radioButtons 面板中没有任何变化(即 "Manual Select" 按钮仍将打开)。相反,我想在取消选中所有 checkboxGroupInput 选项后自动更改 radioButton。

ui.R

library(shiny)

shinyUI(fluidPage(
 title = 'Examples of DataTables',
  sidebarLayout(
   sidebarPanel(

   radioButtons(
    inputId="radio",
    label="Variable Selection Type:",
    choices=list(
      "All",
      "Manual Select"
    ),
    selected="All"),

    conditionalPanel(
     condition = "input.radio != 'All'",
      checkboxGroupInput(
      'show_vars', 
      'Columns in diamonds to show:',
      choices=names(hw), 
      selected = "carat"
    )
   )

  ),
 mainPanel(
  verbatimTextOutput("summary"), 
  tabsetPanel(
    id = 'dataset',
    tabPanel('hw', dataTableOutput('mytable1'))
  )
 )
)
))

server.R(加上我的其他 else if 函数):

library(shiny)
library(ggplot2)
data(diamonds)
hw <- diamonds

shinyServer(function(input, output) {

Data <- reactive({

if(input$radio == "All"){
  hw
} 
else if (length(input$show_vars) == 0){
  hw
}
else {
  hw[,input$show_vars,drop=FALSE]
}

})

output$summary <- renderPrint({
 ## dataset <- hw[, input$show_vars, drop = FALSE]
 dataset <- Data()
 summary(dataset)
})

# a large table, reative to input$show_vars
output$mytable1 <- renderDataTable({
 Data()
 ## hw[, input$show_vars, drop = FALSE]
})
})

也许您正在寻找更新单选按钮的方法 http://shiny.rstudio.com/reference/shiny/latest/updateRadioButtons.html

感谢 Rohit Das,我做到了我想做的。

我是说,

data(diamonds)
hw <- diamonds

shinyServer(function(input, output,session) {

Data <- reactive({

 if(input$radio == "All"){
  hw

 } 
 else if (length(input$show_vars) == 0){
  updateRadioButtons(session,
                       "radio", 
                       "Variable Selection Type:",
                       choices=list("All","Manual Select"),
                       selected = "All")
  updateCheckboxGroupInput(session, 
                           'show_vars', 
                           'Columns in diamonds to  show:',
                           choices=names(hw), 
                           selected = "carat")
 }

 else {
  hw[,input$show_vars,drop=FALSE]
 }

})

shinyWidgets 库有一个名为 pickerInput() 的好函数,它带有 "select all/deselect all" 特性。经过大量研究,我发现这是唯一具有此功能的 Shiny 输入 built-in:

Link 到站点:https://dreamrs.github.io/shinyWidgets/index.html