# Generated by DropToApp library(shiny) library(bslib) # Datasets that come with R datasets <- list( "Cars (mtcars)" = mtcars, "Flowers (iris)" = iris, "Earthquakes (quakes)" = quakes, "Air quality (airquality)" = airquality ) ui <- page_sidebar( title = "Data explorer", sidebar = sidebar( selectInput("dataset", "Dataset", choices = names(datasets)), selectInput("x", "X axis", choices = NULL), selectInput("y", "Y axis", choices = NULL), checkboxInput("trend", "Add a trend line") ), navset_card_tab( nav_panel("Plot", plotOutput("scatter")), nav_panel("Table", DT::DTOutput("table")), nav_panel("Summary", verbatimTextOutput("summary")) ) ) server <- function(input, output, session) { # Inputs: input$dataset, input$x, input$y, input$trend data <- reactive(datasets[[input$dataset]]) # Offer the numeric columns of the chosen dataset as axes observe({ numeric_cols <- names(Filter(is.numeric, data())) updateSelectInput(session, "x", choices = numeric_cols, selected = numeric_cols[1]) updateSelectInput(session, "y", choices = numeric_cols, selected = numeric_cols[2]) }) output$scatter <- renderPlot({ d <- data() req(input$x %in% names(d), input$y %in% names(d)) plot(d[[input$x]], d[[input$y]], xlab = input$x, ylab = input$y, pch = 19, col = "#007bc288") if (input$trend) abline(lm(d[[input$y]] ~ d[[input$x]]), col = "#e74c3c", lwd = 2) }) output$table <- DT::renderDT({ data() }) output$summary <- renderPrint({ summary(data()) }) } shinyApp(ui, server)