library(shiny) library(bslib) # Define UI for slider demo app ---- ui <- page_sidebar( # App title ---- title = "More Widgets", # Sidebar panel for inputs ---- sidebar = sidebar( # Input: Select a dataset ---- selectInput( "dataset", "Choose a dataset:", choices = c("rock", "pressure", "cars") ), # Input: Specify the number of observations to view ---- numericInput("obs", "Number of observations to view:", 10), # Include clarifying text ---- helpText( "Note: while the data view will show only the specified", "number of observations, the summary will still be based", "on the full dataset." ), # Input: actionButton() to defer the rendering of output ---- # until the user explicitly clicks the button (rather than # doing it immediately when inputs change). This is useful if # the computations required to render output are inordinately # time-consuming. actionButton("update", "Update View") ), # Output: Header + summary of distribution ---- h4("Summary"), verbatimTextOutput("summary"), # Output: Header + table of distribution ---- h4("Observations"), tableOutput("view") ) # Define server logic to summarize and view selected dataset ---- server <- function(input, output) { # Return the requested dataset ---- # Note that we use eventReactive() here, which depends on # input$update (the action button), so that the output is only # updated when the user clicks the button datasetInput <- eventReactive( input$update, { switch( input$dataset, "rock" = rock, "pressure" = pressure, "cars" = cars ) }, ignoreNULL = FALSE ) # Generate a summary of the dataset ---- output$summary <- renderPrint({ dataset <- datasetInput() summary(dataset) }) # Show the first "n" observations ---- # The use of isolate() is necessary because we don't want the table # to update whenever input$obs changes (only when the user clicks # the action button) output$view <- renderTable({ head(datasetInput(), n = isolate(input$obs)) }) } # Create Shiny app ---- shinyApp(ui, server)