如何使用actionButton来更改选定的值InputIn R闪亮? [英] How to use an actionButton to change the selected value on a selectInput in R Shiny?

查看:0
本文介绍了如何使用actionButton来更改选定的值InputIn R闪亮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个闪亮的应用程序,它带有一个包含项目列表的精选输入。默认情况下,数据中的所有项目都处于选中状态。我想有2个行动按钮,以帮助用户。一个重置为默认并选择了全部(我已经知道了),而另一个则删除全部并将选择更改为请至少选择一个项目(这实际上等同于删除所有选项,因为没有具有该名称的项目)。这第二个按钮我搞不懂。

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

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)

这篇关于如何使用actionButton来更改选定的值InputIn R闪亮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆