Nicolabo
Nicolabo

Reputation: 1377

Develop Select/Deselect All Button for shiny

Inspired by this solution I just wondering whether is possible to come back to button "All" after you uncheck all columns names. Of course I can simply develop else if function:

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

and result counts all columns in diamonds dataset but in radioButtons panel nothing change (i.e. "Manual Select" button will be still switched on). Instead I would like to change radioButton automatically after uncheck all checkboxGroupInput options.

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 (with my additional else if function):

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

Upvotes: 2

Views: 1845

Answers (3)

Dale Kube
Dale Kube

Reputation: 1460

The shinyWidgets library has a nice function called pickerInput() that comes with a "select all/deselect all" feature. After much research, I found this to be the only Shiny input that has this feature built-in:

enter image description here

Link to site: https://dreamrs.github.io/shinyWidgets/index.html

Upvotes: 0

Nicolabo
Nicolabo

Reputation: 1377

Thanks to Rohit Das I did it wthat I want to.

I mean,

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

})

Upvotes: 0

Rohit Das
Rohit Das

Reputation: 2042

Maybe you are looking for a way to update the radio button http://shiny.rstudio.com/reference/shiny/latest/updateRadioButtons.html

Upvotes: 1

Related Questions