闪亮:使用shinyjs禁用tabPanel()

时间:2020-10-12 19:23:11

标签: javascript r shiny shinyjs

我正在使用@SriPaladugu提供的here答案来禁用tabPanel(),并且我发现如果tabPanel(title,...)中存在空格,它将不起作用。他们编写了自己的JavaScript代码,并使用shinyjs来启用/禁用面板,而我不熟悉JavaScript代码来解决此问题。他们执行此操作的代码存储在对象jscode中;如何修改代码以解决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)


1 个答案:

答案 0 :(得分:1)

我认为您可以按照this answershinyjs::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)

只需完成此答案,即可获得data-value,在浏览器中打开应用程序,执行Ctrl + Shift + C,将鼠标悬停在所需的标签上,并在检查器中检查其data-value参数。 >