How can I edit shiny code so that the one plot can be displayed immediately without having to wait for a second one that takes time?

Hello. I have a dummy app that displays two histograms. The first one is based on only 50 data points while the second one is based on 10 million data points. How can I edit the code so that the first histogram can be displayed without having to wait for the second one?

library(shiny)
library(ggplot2)

# UI
ui <- fluidPage(
  titlePanel("Histogram Comparison"),
  sidebarLayout(
    sidebarPanel(
      actionButton("plotButton", "Generate Plots")
    ),
    mainPanel(
      plotOutput("histogram1"),
      plotOutput("histogram2")
    )
  )
)

# Server
server <- function(input, output) {
  observeEvent(input$plotButton, {
    output$histogram1 <- renderPlot({
      # Generating histogram for 50 observations
      data1 <- data.frame(x = rnorm(50))
      ggplot(data = data1, aes(x = x)) +
        geom_histogram(binwidth = 0.5, fill = "blue", color = "black") +
        labs(title = "Histogram (50 Observations)")
    })

    output$histogram2 <- renderPlot({
      # Generating histogram for 10 million observations

      data2 <- data.frame(x = rnorm(10000000))
      ggplot(data = data2, aes(x = x)) +
        geom_histogram(binwidth = 0.5, fill = "red", color = "black") +
        labs(title = "Histogram (10 Million Observations)")
    })
  })
}

# Run the Shiny app
shinyApp(ui = ui, server = server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.