如何使用 actionButton 更改 R Shiny 中 selectInput 的选定值?

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

我正在构建一个闪亮的应用程序,其中包含一个包含项目列表的 select 输入。默认情况下,数据中的所有项目都是 selected。我想要 2 个操作按钮来帮助用户。一个重置为默认值并拥有所有 selected(我已经弄清楚了),另一个删除所有并改为将 selectInput 更改为“Please Select at Least One Project "(这实际上与删除所有选项相同,因为没有该名称的项目)。第二个按钮我想不通。

我的代码如下,非常感谢任何帮助:

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)

试试这个

  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)