【问题标题】:Enable (disable) a selectInput for a specific tabPanel in tabBox为 tabBox 中的特定 tabPanel 启用(禁用)selectInput
【发布时间】:2018-03-13 12:20:21
【问题描述】:

我有一个这样的闪亮仪表板应用程序构建: 我有一个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")
      }
    })
  }
)

谢谢

【问题讨论】:

  • 您是否尝试过使用conditionalPanel 来简单地隐藏 ui 元素?
  • 请检查我的更新

标签: r shiny shinyjs


【解决方案1】:

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

【讨论】:

  • 感谢您的回答,它可以在闪亮上工作,但在我的情况下什么也没发生。我用 shinybs 解决了警报消息的问题,但这不是一个漂亮的解决方案
  • 你能给我们一个完整的例子吗,仔细看看会有帮助。谢谢
  • 非常感谢! (我在你的第一个例子中没有注意到“价值”)
猜你喜欢
  • 2021-06-19
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
  • 2021-01-27
  • 2020-12-24
  • 2020-08-11
  • 2015-09-24
相关资源
最近更新 更多