select 的操作按钮全部在 Rstudio 的闪亮工具中

Action button with select all in shiny tool of Rstudio

我有以下闪亮的小应用程序。我创建了 select 全部操作按钮。那么当我点击 "select all" 按钮时,如何 select all (A1, ..., C2) 呢?

#ui script
library(shiny)
fluidPage(
    selectizeInput("select", "Select multiple", multiple = T, 
    choices = c("A1", "A2", "B1", "B2", "C1", "C2")),
    actionButton("selectall", "Select all:")
)
#server script
server <- function(input, output){
}

这是你想要的吗?

rm(list = ls())
library(shiny)
mychoices <- c("A1", "A2", "B1", "B2", "C1", "C2")

ui <- fluidPage(
  selectInput("campaigns", "Choose campaign(s):", multiple = T, choices = mychoices),
  actionButton("selectall", "Select all:")
)
server <- function(input, output, session) {
  observeEvent(input$selectall,{
    if (input$selectall%%2 == 0){
      updateSelectInput(session,"campaigns","Choose campaign(s):",choices=mychoices)
    }
    else{
      updateSelectInput(session,"campaigns","Choose campaign(s):",choices=mychoices,selected=mychoices)
    }
  })
}
runApp(list(ui = ui, server = server))