【问题标题】:Shiny updateSelectInput for multiple inputs用于多个输入的闪亮 updateSelectInput
【发布时间】:2016-02-21 20:02:07
【问题描述】:

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

【问题讨论】:

    标签: r shiny-server shiny shinydashboard


    【解决方案1】:

    同意您需要更多信息,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 })
      }
    )
    

    【讨论】:

    • Jason +@Boxuan - 我需要定义所有可能的情况吗? (如果用户选择 A 它应该影响 B 和 C 中的值,那么如果他选择 C ​​它也应该影响 B 等。)另外 - 使用 renderUI/observe 的主要区别是什么?
    • @UserIL 视情况而定 - 您没有真正提供足够的信息让任何人充分回答您的问题。如果您显示您正在尝试的代码并说明您的预期输出,其他人会更容易帮助您。有关renderUIobserve 之间的区别,请参阅:thisthis
    【解决方案2】:

    这里没有足够的信息。但是,根据您的描述,您可以尝试从这里开始。

    shinyServer(function(input, output, session) {
      observe({
        a_option <- input$a_option
        b_option <- input$b_option
        if (a_option == "XXX") {
          updateSelectInput(session, "B", choices = b_options)
          updateSelectInput(session, "C", choices = c_options)
        }
        if (b_option == "XXX") {
          updateOtherInput(session, "input_id", ...)
        }
      })
    })
    

    【讨论】:

      猜你喜欢
      • 2020-05-24
      • 2019-02-15
      • 1970-01-01
      • 2015-11-12
      • 2017-06-29
      • 1970-01-01
      • 2018-07-15
      • 2018-07-21
      • 2021-09-13
      相关资源
      最近更新 更多