gpt4 book ai didi

r - 用于多个输入的 Shiny updateSelectInput

转载 作者:行者123 更新时间:2023-12-04 02:15:52 25 4
gpt4 key购买 nike

我有一个 Shiny 的应用程序,其中包含多个属于层次结构的输入(A -> B -> C)。当用户选择 A - 它应该影响 B 和 C 中的选项。但是,用户只能选择 B(然后它也应该影响其他输入。如果没有选择 - 所有选项都应该可用。我如何实现它?

最佳答案

同意您需要更多信息,observe 模式可能是一个不错的选择。如果您需要在界面中进行更多控制和动态处理,您可以使用带有 renderUI 的动态 UI:

library(shiny)

choices_A <- list("Choice A" = 1, "Choice B" = 2, "Choice C" = 3)
choices_B <- list("Choice B1" = 4, "Choice B2" = 5, "Choice B3" = 6)
choices_C <- list("Choice C1" = 7, "Choice C2" = 8, "Choice C3" = 9)

shinyApp(
ui = fluidPage(
titlePanel("Cascading selects"),
fluidRow(
column(3, wellPanel(
selectInput("select_A", label = "Select A",
choices = choices_A)

)),
column(3, wellPanel(
uiOutput("ui")
)),
column(3, wellPanel(
tags$p("First Input: "),
verbatimTextOutput("value"),
tags$p("Dynamic Input: "),
verbatimTextOutput("dynamic_value")
))
)
),
server = function(input, output) {
output$ui <- renderUI({
if (is.null(input$select_A))
return()

switch(input$select_A,
"1" = selectInput("dynamic", label = "Select A2",
choices = choices_A),
"2" = selectInput("dynamic", label = "Select B",
choices = choices_B),
"3" = selectInput("dynamic", label = "Select C",
choices = choices_C)
)
})
output$value <- renderPrint({ input$select_A })
output$dynamic_value <- renderPrint({ input$dynamic })
}
)

cascading selects

关于r - 用于多个输入的 Shiny updateSelectInput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33807945/

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