R shiny app won't render plots until all multiple tabs was clicked

I built a simple R shiny app with multiple tabs. When I run the app, the plot on each tab would not show when I first clicked on each of the tabs. An error message shows attempt to select less than one element in get1index. However, after I clicked on each of the tabs until the last tab, plots started to show up on each tab without error message. Following is the screenshot I have when I run the app. Would you please kindly point out which direction I should pursue to solve this issue? Thanks,

Before I clicked on each of the tabs, I got the following ERROR message with no plot rendered.
image

After I clicked on each of the tabs, starting from the last tab I clicked, all plots start to show up.

The simplified UI and server part is similar to the following.

ui <- fluidPage(
  titlePanel("EMR Comparative Analysis"),
  tabsetPanel(
    tabPanel("Overall Comparison",
             sidebarLayout(
               sidebarPanel(
                 # Radio button to select between EMR Site and EMR Form
                 radioButtons(
                   inputId = "overall_comparison_type",
                   label = "Select comparison type:",
                   choices = c("EMR Site" = "emrsite", "EMR Form" = "emrform"),
                   selected = "emrsite"
                 ),
                 # Dropdown menu for study selection
                 uiOutput("overallStudySelectInput"),
                 # Input selection for cumulative daily time series
                 uiOutput("overallMetricSelectInput")
               ),
               mainPanel(
                 plotOutput("overallPlot")
               )
             )
    ),
}

# Server ----
server <- function(input, output, session) {
....
  # Render overall Plot
  output$overallPlot <- renderPlot({
    data <- selectedData()$overall
    
    print("overall data structure:")
    print(str(data))
    
    comparison_var <- if (input$overall_comparison_type == "emrsite") {
      "is_emr_site"
    } else {
      "is_emr_form"
    }
    
    comparison_label <- c(
      "Yes" = if (input$overall_comparison_type == "emrsite") "EMR Sites" else "EMR Forms",
      "No" = if (input$overall_comparison_type == "emrsite") "Non-EMR Sites" else "Non-EMR Forms"
    )
    
    data[[comparison_var]] <- factor(data[[comparison_var]], levels = names(comparison_label), labels = comparison_label)
    
    # Identify columns starting with 'ratio' or 'duration_median'
    plot_var <- grep("^ratio|^duration_median", names(data), value = TRUE)
    
    ggplot(data, aes_string(x = comparison_var, y = plot_var, fill = comparison_var)) +
      geom_bar(stat = "identity", position = "dodge") +
      labs(title = "Overall Comparison by Study", x = "EMR vs. Non-EMR", y = "Overall Ratio/Median", fill = "Comparison Group") +
      theme_minimal()
  })
}

After I clicked on each of the tabs, starting from the last tab I clicked, all plots start to show up.

Hi Jane,

Without seeing a little more of the code it would be hard to pin this down. It seems like it might be a situation where a value might be assigned to a variable used in this code after something runs on the other tabs.

My main recommendation, especially if you are writing this code in RStudio, is to throw a browser() call at the start of the renderPlot contents (before data <- selectedData()$overall). When you run this code locally the program will pause once it hits the browser call and take you back to the console (lower left part of the RStudio window). You can then step through each line of code and see in the environmental variables (upper right part of the RStudio window) what values are being assigned to variables in the local, reactive environment. You should also be able to see which part of the renderPlot call is throwing that error.