代码之家  ›  专栏  ›  技术社区  ›  J.Sabree

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

  •  0
  • J.Sabree  · 技术社区  · 5 年前

    我正在构建一个闪亮的应用程序,其中包含一个项目列表。默认情况下,将选中数据中的所有项目。我想有2个动作按钮来帮助用户。一个重置为默认值并选择了所有选项(我已经找到了),另一个删除了所有选项,而是将selectInput更改为“请至少选择一个项目”(这实际上与删除所有选项相同,因为没有具有该名称的项目)。第二个按钮我搞不懂。

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

    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)
    
    0 回复  |  直到 5 年前
        1
  •  2
  •   YBS    5 年前

    试试这个

      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)
    
    推荐文章