如何解决如何使用 actionButton 更改 R Shiny 中 selectInput 上的选定值?
我正在构建一个闪亮的应用程序,其中包含一个包含项目列表的选择输入。默认情况下,选择数据中的所有项目。我想要 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",),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",),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,selected=sort(unique(test$project)) )
})
}
shinyApp(ui = ui,server = server)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。