为 tabBox 中的特定 tabPanel 启用(禁用)selectInput

Enable (disable) a selectInput for a specific tabPanel in tabBox

我有一个像这样构建的 shinydashboard 应用程序: 我有一个 tabItem,其中有一个框(带有我的 selectInput)和一个 tabBox。在框中,我有不同的过滤器,在 tabBox 中,我有两个 tabPanel(tab1 和 tab2)。 我想要的是禁用 tab1 的 selectInput(只有一个)并为 tab2 启用它。 我正在尝试使用 shinyJS 包来完成它,但我遇到了一些困难。

library(shiny)
library(shinydashboard)
library(shinyjs)

df <- data.frame(id = 1:10, name = paste(letters[1:10], sep = ""))
body <- dashboardBody(
  shinyjs::useShinyjs(),
  fluidRow(
    tabBox(
      title = "First tabBox",
      # The id lets us use input$tabset1 on the server to find the current tab
      id = "tabset", height = "250px",
      tabPanel("tab1", "First tab content"),
      tabPanel("tab2", "Tab content 2")
    ),
    box(title = "Variables filter",
        id = "filter_box",
        br(),
        background = "light-blue",
        solidHeader = TRUE,
        width = 2,
        selectInput("filter_id", "Choose id", multiple = T, choices = c("All", as.character(unique(df$id)))))
  )
)

shinyApp(
  ui = dashboardPage(
    dashboardHeader(title = "tabBoxes"),
    dashboardSidebar(),
    body
  ),
  server = function(input, output) {
    observe({
      validate(need(!is.null(input$tab1), ""))
      if (input$tab1 == 1) {
        disable("filter_id")
      } else {
        enable("filter_id")
      }
    })
  }
)

谢谢

我改了答案。下面的代码现在应该可以工作了。您必须为每个 tabPanel 添加一个值,之后您可以参考该值。另见此处:https://www.rdocumentation.org/packages/shinydashboard/versions/0.6.1/topics/tabBox

library(shiny)
library(shinydashboard)
library(shinyjs)

df <- data.frame(id = 1:10, name = paste(letters[1:10], sep = ""))
body <- dashboardBody(
  shinyjs::useShinyjs(),
  fluidRow(
    tabBox(
      title = "First tabBox",
      # The id lets us use input$tabset1 on the server to find the current tab
      id = "tabset", height = "250px",
      tabPanel("tab1", value=1,"First tab content"),
      tabPanel("tab2", value=2,"Tab content 2")
    ),
    box(title = "Variables filter",
        id = "filter_box",
        br(),
        background = "light-blue",
        solidHeader = TRUE,
        width = 2,
        selectInput("filter_id", "Choose id", multiple = T, choices = c("All", as.character(unique(df$id)))))
  )
)

shinyApp(
  ui = dashboardPage(
    dashboardHeader(title = "tabBoxes"),
    dashboardSidebar(),
    body
  ),
  server = function(input, output) {
    observe({
      validate(need(!is.null(input$tabset), ""))
      if (input$tabset == 1) {
        disable("filter_id")
      } else {
        enable("filter_id")
      }
    })
  }
)