How to use updateTabItems with eventReactive


#1

Hi,
I started from this example (https://shiny.rstudio.com/articles/tabsets.html) and tried to add a submit button to change active tab and show the “Plot” tab when user click on “Submit” button.
The button doesn’t work and the active tab do not change. Can someone explain me why and how to do it? Thanks

# Define UI for random distribution app ----
ui <- fluidPage(
  
  # App title ----
  titlePanel("Tabsets"),
  
  # Output: Tabset w/ plot, summary, and table ----
  tabsetPanel(type = "tabs",
      tabPanel("Conf", 
               # Input: Select the random distribution type ----
               radioButtons("dist", "Distribution type:",
                            c("Normal" = "norm",
                              "Uniform" = "unif",
                              "Log-normal" = "lnorm",
                              "Exponential" = "exp")),
               
               # br() element to introduce extra vertical spacing ----
               br(),
               
               # Input: Slider for the number of observations to generate ----
               sliderInput("n",
                           "Number of observations:",
                           value = 500,
                           min = 1,
                           max = 1000),
                actionButton("submit","Submit")
               ),
      tabPanel("Plot", plotOutput("plot")),
      tabPanel("Summary", verbatimTextOutput("summary")),
      tabPanel("Table", tableOutput("table"))
  )
)

# Define server logic for random distribution app ----
server <- function(session, input, output) {
  
  # Reactive expression to generate the requested distribution ----
  # This is called whenever the inputs change. The output functions
  # defined below then use the value computed from this expression
  d <- eventReactive(input$submit,{
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)
    
    dist(input$n)
    updateTabItems(session, "tabs", "Plot")
  })
  
  # Generate a plot of the data ----
  # Also uses the inputs to build the plot label. Note that the
  # dependencies on the inputs and the data reactive expression are
  # both tracked, and all expressions are called in the sequence
  # implied by the dependency graph.
  output$plot <- renderPlot({
    dist <- input$dist
    n <- input$n
    
    hist(d(),
         main = paste("r", dist, "(", n, ")", sep = ""),
         col = "#75AADB", border = "white")
  })
  
  # Generate a summary of the data ----
  output$summary <- renderPrint({
    summary(d())
  })
  
  # Generate an HTML table view of the data ----
  output$table <- renderTable({
    d()
  })
  
}

shinyApp(ui, server)

#2

I found my mistakes:

  • I use updateTabItems instead of updateTabsetPanel
  • I have to use this in an observeEvent not in eventReactive

Here is the correct version:

library(shiny)

# Define UI for random distribution app ----
ui <- fluidPage(
  
  # App title ----
  titlePanel("Tabsets"),
  
  # Output: Tabset w/ plot, summary, and table ----
  tabsetPanel(id = "inTabset", type = "tabs",
              tabPanel("Conf",value="tab1", 
                       # Input: Select the random distribution type ----
                       radioButtons("dist", "Distribution type:",
                                    c("Normal" = "norm",
                                      "Uniform" = "unif",
                                      "Log-normal" = "lnorm",
                                      "Exponential" = "exp")),
                       
                       # br() element to introduce extra vertical spacing ----
                       br(),
                       
                       # Input: Slider for the number of observations to generate ----
                       sliderInput("n",
                                   "Number of observations:",
                                   value = 500,
                                   min = 1,
                                   max = 1000),
                       actionButton("submit","Submit")
              ),
              tabPanel("Plot", value="tab2", plotOutput("plot")),
              tabPanel("Summary", value="tab3", verbatimTextOutput("summary")),
              tabPanel("Table", value="tab4", tableOutput("table"))
  )
)

# Define server logic for random distribution app ----
server <- function(session, input, output) {
  
  # Reactive expression to generate the requested distribution ----
  # This is called whenever the inputs change. The output functions
  # defined below then use the value computed from this expression
  d <- eventReactive(input$submit,{
    dist <- switch(input$dist,
                   norm = rnorm,
                   unif = runif,
                   lnorm = rlnorm,
                   exp = rexp,
                   rnorm)
    
    dist(input$n)
  })
  
  observeEvent(input$submit,{
    updateTabsetPanel(session, "inTabset", selected = "tab2")
  })
  
  # Generate a plot of the data ----
  # Also uses the inputs to build the plot label. Note that the
  # dependencies on the inputs and the data reactive expression are
  # both tracked, and all expressions are called in the sequence
  # implied by the dependency graph.
  output$plot <- renderPlot({
    dist <- input$dist
    n <- input$n
    
    hist(d(),
         main = paste("r", dist, "(", n, ")", sep = ""),
         col = "#75AADB", border = "white")
  })
  
  # Generate a summary of the data ----
  output$summary <- renderPrint({
    summary(d())
  })
  
  # Generate an HTML table view of the data ----
  output$table <- renderTable({
    d()
  })
  
}

shinyApp(ui, server)