0

I have a shinydashboard app build like this : I have a tabItem where I got a box (with my selectInput) and a tabBox. In the box I have my different filters and in the tabBox I have two tabPanel (tab1 and tab2). What I want is to disable the selectInput (only one) for the tab1 and enable it for the tab2. I am trying to do it with the shinyJS package but I got somme difficulties.

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")
      }
    })
  }
)

Thanks

Mostafa90
  • 1,674
  • 1
  • 21
  • 39

1 Answers1

4

I changed my answer. The following code should work now. You have to add a value to each tabPanel, to which you can refer afterwards. See also here: 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")
      }
    })
  }
)
thmschk
  • 614
  • 3
  • 16
  • Thanks for your answer it's work on shiny but in my case nothing happened. I solved the problem with alert message with shinybs but it's not a beautiful solution – Mostafa90 Oct 04 '17 at 11:59
  • Could you give us a full example, it would be helpful to take a closer look. Thanks – thmschk Oct 05 '17 at 08:09
  • Thanks so much ! (I didnt noticed "value" in your first example) – Mostafa90 Oct 14 '17 at 21:41