【问题标题】:Show / Hide selectinput in R shiny based on tabpanel selection基于选项卡面板选择在 R Shiny 中显示/隐藏 selectinput
【发布时间】:2021-07-09 17:41:48
【问题描述】:

我需要为不同的标签显示不同的下拉/选择输入。

例如。如果选择了第一个选项卡,则显示带有值列表的 selectinput Tim,Bill,Jeff,... 如果选择了选项卡二,则显示带有值列表的 selectinput Cat、Dog、Squirrel、...

我在网上找到了以下脚本,但反之亦然(根据选择输入选择显示/隐藏选项卡面板 - 但我需要根据选项卡面板选择显示/隐藏选择输入)。

runApp(list(
  ui = shinyUI(
    fluidPage(
      
      sidebarLayout(
        sidebarPanel(
          selectInput(
            inputId = 'selected.indicator',
            label = 'Select an option: ',
            choices = c('mean', 'median', 'mode')
          )
        ),
        mainPanel(
          tabsetPanel(
            tabPanel("Prevalence / mean", value = 'meanTab', DT::dataTableOutput("prevtab")),
            tabPanel("Subgroups comparison", value = 'medianTab',  DT::dataTableOutput("comptab")),
            id ="tabselected"
          )
        )
      )
    )
  ),
  
  server = function(input, output, session) {
    
    observe({
      req(input$selected.indicator)
      if (grepl("MEDIAN", toupper(input$selected.indicator), fixed = TRUE)) {
        hideTab(inputId = "tabselected", target = 'medianTab')
      }
      else showTab(inputId = "tabselected", target = 'medianTab')
    })
  }
))

【问题讨论】:

    标签: r shiny shinydashboard shinyapps shiny-reactivity


    【解决方案1】:

    给你。

    runApp(list(
      ui = shinyUI(
        fluidPage(
          
          sidebarLayout(
            sidebarPanel(
              selectInput(
                inputId = 'selected.indicator',
                label = 'Select an option: ',
                choices = c('')
              )
            ),
            mainPanel(
              tabsetPanel(
                tabPanel("tab1", value = 'tab1', p("tab1")),
                tabPanel("tab2", value = 'tab2',  p("tab2")),
                id ="tabselected"
              )
            )
          )
        )
      ),
      
      server = function(input, output, session) {
        choices <- reactiveValues(
          tab1 = c('Tim', 'Bill', 'Jeff'),
          tab2 = c('Cat', 'Dog', 'Squirrel')
        )
        observeEvent(input$tabselected, {
          updateSelectInput(session, 'selected.indicator', choices = choices[[input$tabselected]])
        })
      }
    ))
    

    【讨论】:

    • 非常感谢!!!上面的脚本效果很好。需要一个小的调整。如果两个 inputselect 值都是从表中派生的,如何替换“choices = c('')”部分?前任。 tab1 = table1$column1 和 tab2 = table2$column1 而不是 c('Tim'...)
    • 请忽略。我懂了。它只是你的脚本。只需要调整输入选择源。非常感谢您的支持!!!
    猜你喜欢
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    相关资源
    最近更新 更多