[英]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,...)
.我正在使用此处提供的@SriPaladugu 的答案来禁用tabPanel()
,我注意到如果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.他们编写了自己的 JavaScript 代码并使用了shinyjs
来启用/禁用面板,而我不熟悉解决此问题的 JavaScript 代码。 Their code that does this is stored in the object jscode
;他们执行此操作的代码存储在对象jscode
; how can the code be modified to account for spaces in tabPanel(title,...)
?如何修改代码以考虑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)
I think you can simplify your code, following this answer and the documentation of shinyjs::disable()
:我认为你可以简化你的代码,遵循这个答案和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.只是为了完成这个答案,要获取data-value
,在浏览器中打开应用程序,按 Ctrl+Shift+C,将鼠标悬停在所需的选项卡上,然后在 Inspector 中检查它的data-value
参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.