gpt4 book ai didi

R Shiny checkboxGroupInput - 单击选择所有复选框

转载 作者:行者123 更新时间:2023-12-04 05:38:08 24 4
gpt4 key购买 nike

我有一个包含 checkboxGroupInput 的 R Shiny 应用程序,我正在尝试使用 updateCheckboxGroupInput 函数创建一个“全选”按钮。
你可以在下面看到完整的代码,但基本上我是这样定义 cb 组的:

checkboxGroupInput("campaigns","Choose campaign(s):",campaigns_list)

然后,单击按钮,调用函数:
updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list,selected=campaigns_list)

我有迹象表明该函数已运行,但它实际上是取消选中复选框。
顺便说一句,当我将选择放在定义 cbGroupInput 上时,它确实起作用了,但在函数上不起作用。

谢谢!

这是我的 server.R:
library(shiny)
source('usefulFunctions.R')
shinyServer(function(input, output, session) {

output$cascading <- renderUI({
provider_id <- input$provider
if (provider_id == "") return(NULL)
campaigns_list <<- t(getCampaigns(provider_id))
tagList(
checkboxGroupInput("campaigns","Choose campaign(s):",
choices = campaigns_list, selected = campaigns_list),
actionLink("selectall","Select All")
)
})

observe({
if(is.null(input$selectall)) return(NULL)
if (input$selectall > 0)
{
print(campaigns_list)
updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list,selected=campaigns_list)
}
})


})

最佳答案

我还通过检查按钮或链接是否可以被 2 整除,在这里添加了选择和取消选择选项

#rm(list = ls())
library(shiny)
campaigns_list <- letters[1:10]

ui =fluidPage(
checkboxGroupInput("campaigns","Choose campaign(s):",campaigns_list),
actionLink("selectall","Select All")
)
server = function(input, output, session) {

observe({
if(input$selectall == 0) return(NULL)
else if (input$selectall%%2 == 0)
{
updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list)
}
else
{
updateCheckboxGroupInput(session,"campaigns","Choose campaign(s):",choices=campaigns_list,selected=campaigns_list)
}
})
}
runApp(list(ui = ui, server = server))

关于R Shiny checkboxGroupInput - 单击选择所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28829682/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com