Search code examples
javascriptrshinyshinyjs

Shiny: Disable tabPanel() using shinyjs


I'm using @SriPaladugu's answer provided here to disable a tabPanel() and I noticed that it doesn't work if there exist a space in tabPanel(title,...). They wrote their own JavaScript code and used shinyjs to enable/disable panels and I'm not familiar with JavaScript code to fix this issue. Their code that does this is stored in the object jscode; how can the code be modified to account for spaces in tabPanel(title,...)?

library(shiny)
library(shinyjs)

jscode <- "
shinyjs.disableTab = function(name) {
var tab = $('.nav li a[data-value=' + name + ']');
tab.bind('click.tab', function(e) {
e.preventDefault();
return false;
});
tab.addClass('disabled');
}

shinyjs.enableTab = function(name) {
var tab = $('.nav li a[data-value=' + name + ']');
tab.unbind('click.tab');
tab.removeClass('disabled');
}
"
css <- "
.nav li a.disabled {
background-color: #aaa !important;
color: #333 !important;
cursor: not-allowed !important;
border-color: #aaa !important;
}"



ui <- shinyUI(fluidPage(
  shinyjs::useShinyjs(),
  shinyjs::extendShinyjs(text = jscode, functions = c("disableTab","enableTab")),
  shinyjs::inlineCSS(css),
  navbarPage("Test",id="navbarPage",
             tabPanel("FirstTab", id = "first_tab",
                      sidebarLayout(
                        sidebarPanel(),
                        mainPanel()
                      )
             ),
             tabPanel("Secondtab", id = "second_tab",
                      sidebarLayout(
                        sidebarPanel(),
                        mainPanel()
                      )
             ),
             tabPanel("Third tab", id = "third_tab",
                      sidebarLayout(
                        sidebarPanel(),
                        mainPanel()
                      )
             )
             )
))

server <- shinyServer(function(input, output, session) {
  # disable tabs Exposure, Covariate, and Construct on page load
  shinyjs::js$disableTab("Secondtab")
  shinyjs::js$disableTab("Third tab")
})

# Run the application
shinyApp(ui = ui, server = server)



Solution

  • I think you can simplify your code, following this answer and the documentation of shinyjs::disable():

    library(shiny)
    library(shinyjs)
    
    css <- "
    .nav li a.disabled {
    background-color: #aaa !important;
    color: #333 !important;
    cursor: not-allowed !important;
    border-color: #aaa !important;
    }"
    
    ui <- shinyUI(fluidPage(
      shinyjs::useShinyjs(),
      shinyjs::inlineCSS(css),
      navbarPage("Test",id="navbarPage",
                 tabPanel("FirstTab", id = "first_tab",
                          sidebarLayout(
                            sidebarPanel(),
                            mainPanel()
                          )
                 ),
                 tabPanel("Secondtab", id = "second_tab",
                          sidebarLayout(
                            sidebarPanel(),
                            mainPanel()
                          )
                 ),
                 tabPanel("Third tab", id = "third_tab",
                          sidebarLayout(
                            sidebarPanel(),
                            mainPanel()
                          )
                 )
      )
    ))
    
    server <- shinyServer(function(input, output, session) {
      # disable tabs Exposure, Covariate, and Construct on page load
      shinyjs::disable(selector = '.navbar-nav a[data-value="Secondtab"')
      shinyjs::disable(selector = '.navbar-nav a[data-value="Third tab"')
    })
    
    # Run the application
    shinyApp(ui = ui, server = server)
    

    Just to complete this answer, to obtain data-value, open the app in browser, do Ctrl+Shift+C, hover the tab you want and check its data-value argument in the Inspector.