Search code examples
rshinyshinydashboardshiny-reactivityshinyjs

How to use an actionButton to change the selected value on a selectInput in R Shiny?


I'm building a shiny app with a select input that has a list of projects. By default, all the projects in the data are selected. I want to have 2 action buttons to help users. One which resets to the default and has all selected (which I've figured out) and another that removes all and instead changes the selectInput to "Please Select at Least One Project" (which effectively is the same as removing all options because there's no project with that name). This second button I cannot figure out.

My code is below and any help is greatly appreciated:

library(shiny)
library(shinyjs)

test <- tibble(project = c("Justin", "Corey","Sibley"),
               april_2021 = c(10, 100, 101),
               may_2021 = c(1, 4, 7))

ui <- fluidPage(
    useShinyjs(),

    sidebarLayout(
        sidebarPanel(
            div(id = "project_inputs",
                selectInput(inputId = "filter_by_project",
                        label = "Filter by Project",
                        choices = c(sort(unique(test$project)), "Please Select at Least One Project"),
                        multiple = TRUE,
                        selected = sort(unique(test$project)))),
#I can't figure out the remove_all button
            actionButton(inputId = "remove_all", 
                         label = "Unselect All Projects", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B"),
            actionButton(inputId = "add_all", 
                         label = "Select All Projects", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B"),
        ),

        mainPanel(
        )
    )
)

server <- function(input, output) {
    
#This is where the remove_all will go

    observeEvent(input$remove_all, {
        reset("add_all")
    })
}

shinyApp(ui = ui, server = server)

Solution

  • Try this

      library(shiny)
      library(shinyjs)
      
      test <- tibble(project = c("Justin", "Corey","Sibley"),
                     april_2021 = c(10, 100, 101),
                     may_2021 = c(1, 4, 7))
      
      ui <- fluidPage(
        useShinyjs(),
        
        sidebarLayout(
          sidebarPanel(
            div(id = "project_inputs",
                selectizeInput(inputId = "filter_by_project",
                            label = "Filter by Project",
                            choices = sort(unique(test$project)), 
                            multiple = TRUE,
                            selected = unique(test$project)[1] )),
            
            #I can't figure out the remove_all button
            actionButton(inputId = "remove_all", 
                         label = "Unselect All Projects", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B"),
            actionButton(inputId = "add_all", 
                         label = "Select All Projects", style = "color: #FFFFFF; background-color: #CA001B; border_color: #CA001B"),
          ),
          
          mainPanel(
          )
        )
      )
      
      server <- function(input, output, session) {
        
        observeEvent(input$remove_all, {
          updateSelectizeInput(session,"filter_by_project",choices=sort(unique(test$project)), 
                            selected=NULL, options = list(placeholder="Please Select at Least One Project")
                            )
        })
        observeEvent(input$add_all, {
          updateSelectizeInput(session,"filter_by_project",choices=sort(unique(test$project)), selected=sort(unique(test$project)) )
        })
      }
      
      shinyApp(ui = ui, server = server)