10.3 Dynamic visibility

To show and hide parts of the UI dynamically and interactively.


The tabsetPanel() is the second function for this chapter and it involves the visibility of part of the app.

This second function is made to let the user show and/or hide some of the tabs set in the main panel. It is a technique that allows managing the appearance of the app with selecting visibility of the tabs as shown in the Tabsets section of the Shiny.rstudio.com guide’s page of the website.

To enhance your app with features and the most wanted themes and as an example on how to set the visibility of a tab in your app, you can follow the steps found here: Gallery

As an example here is shown how to switch between panels hiding one panel and showing the other with different content.

library(shiny)

##################  switcher ##############################

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput("controller", "Show", choices = c("plot","summary"))
    ),
    mainPanel(
      tabsetPanel(
        id = "switcher",
        type = "hidden",
        tabPanelBody("plot", "Plot"),
        tabPanelBody("summary", "Summary")
        
      )
    )
  )
)

server <- function(input, output, session) {
  
  observeEvent(input$controller, {
    updateTabsetPanel(inputId = "switcher", selected = input$controller)
  })
  
}

Finally, the technique of the Conditional UI allows you to simulate different parameters to be set in the app. The tabsetPanel() is updated with input requests as a separate section with different types of [id]Input() and then embedded inside a fuller UI.

Example of a chunk of code to be integrated in the UI:

parameter_tabs <- tabsetPanel(
  
  tabPanel("normal",
    numericInput("mean", "mean", value = 1)
  )
)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      
      numericInput("n", "Number of samples", value = 100),
      
      parameter_tabs # where to set the chunk of code
    ),
    mainPanel(
      plotOutput("hist")
    )
  )
)

There are other options to use to be able to switch among different pages, an example of this is creating a wizard and using the switch_page() function, this will be shown in Chapter 18 - Functions as said in the book.