Skip to contents

dv.manager includes a set of internal utilities to improve module interactivity.

switch_tab utility

This function allows a developer to switch to another module from within their module.

We include below an example of such an app.

# - name: a title to display inside the module
# - id: shiny id

switch_ui <- function(id, name) {
  ns <- shiny::NS(id)
  shiny::tagList(
    shiny::h1(name),
    shiny::actionButton(ns("switch"), "Switch tab")
  )
}

# - id the shiny module id
# - selected The name of the tab that we want to switch to
# - switch_func the switching function

switch_server <- function(id, selected, switch_func) {
  shiny::moduleServer(
    id,
    function(input, output, session) {
      shiny::observeEvent(input$switch, {
        switch_func(selected)
      })
    }
  )
}

# - name A title that will be shown inside the module
# - selected The name of the tab that we want to switch to
# - module_id shiny module ID

mod_switch <- function(name, selected, module_id) {
  mod <- list(
    ui = function(module_id) {
      switch_ui(module_id, name)
    },
    server = function(afmm) {
      switch_server(module_id, selected, afmm[["utils"]][["switch_function"]])
    },
    module_id = module_id
  )
  mod
}

app <- function() {
  dv.manager::run_app(
    data = list(),
    module_list = list(
      "Mod 1" = mod_switch(
        name = "Mod 1",
        # Note that in this case we make reference to the name of the entry in the list and not to the module_id.
        # This functionality is based on updateTabsetpanel that works with the tab titles.
        selected = "Mod 2",
        module_id = "mod1"
      ),
      "Mod 2" = mod_switch(
        name = "Mod 2",
        selected = "Mod 1",
        module_id = "mod2"
      )
    ),
    filter_data = "",
    filter_key = ""
  )
}

app()