dropdownButton
中的shinyWidgets
函数很棒,因为我可以使用它轻松地创建{/ {3}}之类的checkboxGroupInput
中的所有选项。
但是,假设我想将这两个dropdownButton
中的两个(或多个)放在一起,放在另一个dropdownButton
中。我似乎无法完成这项工作。这是我的尝试。 (我正在ui
脚本中构建server.R
项,因为在我的实际项目中,我需要它们以这种方式工作。)
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
titlePanel("Dropsdowns in your Dropdowns"),
sidebarLayout(sidebarPanel(
uiOutput("io_c1"),
uiOutput("io_c2"),
uiOutput("io_combine")
),
mainPanel())
)
server <- function(input, output, session) {
## drop down 1 items ##
# drop down 1 choices
c1 <- checkboxGroupInput(
inputId = "check1",
label = "pick carb",
choices = unique(mtcars$carb),
selected = unique(mtcars$carb)
)
# select all / none drop down 1
sa1 <- actionButton(inputId = "c1_check_all",
label = "(un)select all")
# set up observe on check box 1
observeEvent(input$c1_check_all, {
if (is.null(input$check1)) {
updateCheckboxGroupInput(
session = session,
inputId = "check1",
selected = unique(mtcars$carb)
)
} else {
updateCheckboxGroupInput(
session = session,
inputId = "check1",
selected = ""
)
}
})
# put all / none button and check box 1 into a dropdownbutton
output$io_c1 <- renderUI({
dropdownButton(sa1,
c1,
circle = FALSE,
label = "Carbs",
status = "default")
})
## drop down 2 items ##
# drop down 2 choices
c2 <- checkboxGroupInput(
inputId = "check2",
label = "pick gear",
choices = unique(mtcars$gear),
selected = unique(mtcars$gear)
)
# select all / none drop down 2
sa2 <- actionButton(inputId = "c2_check_all",
label = "(un)select all")
# set up observe on check box 2
observeEvent(input$c2_check_all, {
if (is.null(input$check2)) {
updateCheckboxGroupInput(
session = session,
inputId = "check2",
selected = unique(mtcars$gear)
)
} else {
updateCheckboxGroupInput(
session = session,
inputId = "check2",
selected = ""
)
}
})
# put all / none button and check box 2 into a dropdownbutton
output$io_c2 <- renderUI({
dropdownButton(sa2,
c2,
circle = FALSE,
label = "Gears",
status = "default")
})
## now try to make this all one thing ##
d1 <- dropdownButton(sa1,
c1,
circle = FALSE,
label = "Carbs",
status = "default")
d2 <- dropdownButton(sa2,
c2,
circle = FALSE,
label = "Gears",
status = "default")
output$io_combine <-
renderUI(dropdownButton(
d1,
d2,
circle = FALSE,
label = "Drop Downs Here",
status = "default"
))
}
shinyApp(ui = ui, server = server)
因此,我将其与每个单独的dropdownButton
一起使用,但是当我尝试将结果全部放到代码的第三部分中时,它不起作用。 dropdownButton
已创建,并且两个单独的dropdownButton
位于其中,但它们根本不响应。有什么想法可能会丢失吗?