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

时间:2017-10-02 12:19:27

标签: r shiny shinyjs

我有一个像这样的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")
      }
    })
  }
)

由于

1 个答案:

答案 0 :(得分:3)

我改变了答案。以下代码现在应该可以使用。您必须为每个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")
      }
    })
  }
)