I am trying to build a shiny app where tabs of the app should get enabled by a action button. I am using shiny modules to build the application. But I am understanding how could I share the inputID of a module to the server function of the app.
Please consider the following code that I am trying:
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');
}
shinyjs.changeToTab = function(params){
var defaultParams = {
tab : null
};
params = shinyjs.getParams(params, defaultParams)
var tab = $('.nav li a[data-value=' + params.tab + ']');
console.log(tab);
tab.trigger('click');
}"
css <-
".nav li a.disabled {
background-color: #aaa !important;
color: #333 !important;
cursor: not-allowed !important;
border-color: #aaa !important;
}"
defineUI <- function(id){
tagList(
actionButton(NS(id, 'btn'), 'Select')
)
}
ui <- fluidPage(
shinyjs::useShinyjs(),
shinyjs::extendShinyjs(text = jscode, functions = c('disableTab','enableTab', 'changeToTab')),
shinyjs::inlineCSS(css),
fluidRow(
column(2, defineUI('actBtn')),
column(10, tabsetPanel(
tabPanel('tab1'),
tabPanel('tab2'),
tabPanel('tab3')
))
)
)
server <- function(input, output, sessio){
shinyjs::js$disableTab('tab2')
shinyjs::js$disableTab('tab3')
observeEvent(input$btn, {
shinyjs::js$enableTab("tab2")
shinyjs::js$enableTab('tab3')
shinyjs::js$changeToTab('tab2')
})
}
shinyApp(ui, server)