Share inputID of a module to server of a shiny app

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)

a module is more than a UI, its a UI and Server code in combination. so add

defineServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      reactive(input$btn)
    }
  )
}

this will pass through the input button that the module ui defines as a reactive return.

Then modify your app's server code:


server <- function(input, output, sessio) {
  btn_from_module <- defineServer(id = "actBtn")
  
  shinyjs::js$disableTab("tab2")
  shinyjs::js$disableTab("tab3")

  observeEvent(btn_from_module(), {
    str(btn_from_module())
    shinyjs::js$enableTab("tab2")
    shinyjs::js$enableTab("tab3")
    shinyjs::js$changeToTab("tab2")
  })
}

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.