Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue with Accessing tabsetPanel Value in Shiny Module

I am encountering an issue trying to access the selected tab from a tabsetPanel in a Shiny module and was hoping to get some assistance.

Here’s a simplified version of my code:

library(shiny)

# Define UI
my_module_ui <- function(id) {
  ns <- NS(id)
  fluidPage(
    tabsetPanel(
      id = ns("tabSelector"),
      tabPanel("Land", value = "land", "Land Content"),
      tabPanel("Air", value = "air", "Air Content")
    )
  )
}

# Define Server
my_module_server <- function(output, input, session, notif_react) {
  ns <- session$ns

  observe({
    print(input$tabSelector)
  })
}

# App UI
ui <- fluidPage(
  my_module_ui("my_module")
)

# App Server
server <- function(input, output, session) {
  callModule(my_module_server, "my_module")
}

# Run App
shinyApp(ui, server)

When I run the app, it should theoretically print the value of the selected tab ("land" or "air") whenever it changes. However, this is not happening and I’m not receiving any output.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’m observing the tabSelector and it should print its value. Unfortunately, I’m receiving the following error:

Warning: Error in $: Can't read output 'my_module-tabSelector'

>Solution :

There are multiple issues with your code:

  • You are defining your module server function incorrectly
  • The parameters of your your "module server" are incorrectly ordered
  • You are calling you module server incorrectly

Please read the online documentation on Shiny modules. A good start is here.

Here is a working version of your code.

library(shiny)

# Define UI
my_module_ui <- function(id) {
  ns <- NS(id)
    tabsetPanel(
      id = ns("tabSelector"),
      tabPanel("Land", value = "land", "Land Content"),
      tabPanel("Air", value = "air", "Air Content")
    )
}

# Define Server
my_module_server <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      ns <- session$ns

      observe({
        print(input$tabSelector)
      })
    }
  )
}

# App UI
ui <- fluidPage(
  my_module_ui("my_module")
)

# App Server
server <- function(input, output, session) {
  my_module_server("my_module")
}

# Run App
shinyApp(ui, server)

Personally, I’d use observeEvent rather than observe for exactly the same reasons that I mentioned yesterday.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading