Select / Deselect all buttons to select a shiny variable.

I have this statement that allows me to get basic descriptive statistics about my variables:

checkboxGroupInput('show_vars', 'Columns in diamonds to show:', names(input_data), selected = names(input_data)) 

However, after I had to cancel 10 variables in order to get one variable that I was interested in, I realized that this user interface is not very friendly. I would like to add a button that selects / deselects all when you click on it. It can be clicked several times. I don’t even know how to start. Any boosts will help.

ui.R:

 library(shiny) hw<-diamonds shinyUI(fluidPage( title = 'Examples of DataTables', sidebarLayout( sidebarPanel( checkboxGroupInput('show_vars', 'Columns in diamonds to show:', names(hw), selected = names(hw)) ), mainPanel( verbatimTextOutput("summary"), tabsetPanel( id = 'dataset', tabPanel('hw', dataTableOutput('mytable1')) ) ) ) )) 

server.R:

 library(shiny) data(diamonds) hw<-diamonds shinyServer(function(input, output) { output$summary <- renderPrint({ dataset <- hw[, input$show_vars, drop = FALSE] summary(dataset) }) # a large table, reative to input$show_vars output$mytable1 <- renderDataTable({ library(ggplot2) hw[, input$show_vars, drop = FALSE] }) }) 
+6
source share
3 answers

I added global.R to download packages and data - not always necessary, but usually cleaner. There may be different ways to do what I did below, but I tend to use conditional panels in such situations.

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

 library(shiny) library(ggplot2) ## shinyServer(function(input, output) { Data <- reactive({ if(input$radio == "All"){ 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] }) }) 

global.R

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

enter image description here

enter image description here

+8
source

This is how I set the select / deselect all button.

In ui.R, add an action button if necessary:

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

Then server.R uses updateCheckboxGroupInput based on the condition of the action button. If the number of button presses is equal, then it will select everything; otherwise, if it is odd, it will not select none.

 # select/deselect all using action button 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(hw))) } 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()) }} }) 

The complete application for your example is below - you need to add a session to the server function, I added a condition for renderDataTable when no variables are selected.

 library(shiny) library(ggplot2) data(diamonds) hw <- diamonds runApp( list( ui=( fluidPage( title = 'Examples of DataTables', sidebarLayout( sidebarPanel( actionButton("selectall", label="Select/Deselect all"), checkboxGroupInput('show_vars', 'Columns in diamonds to show:', names(hw), selected = names(hw)) ), mainPanel( verbatimTextOutput("summary"), tabsetPanel( id = 'dataset', tabPanel('hw', dataTableOutput('mytable1')) ))))), server = (function(input, output, session) { output$summary <- renderPrint({ dataset <- hw[, input$show_vars, drop = FALSE] summary(dataset) }) 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(hw))) } 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()) }} }) # a large table, reative to input$show_vars output$mytable1 <- renderDataTable({ if (is.null(input$show_vars)){ data.frame("no variables selected" = c("no variables selected")) } else{ hw[, input$show_vars, drop = FALSE] } }) }) )) 
+8
source

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

enter image description here

Link to the site: https://dreamrs.imtqy.com/shinyWidgets/index.html

+1
source

Source: https://habr.com/ru/post/972716/


All Articles