Create an application that has a text field to enter a URL (default value: https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv). The app should read the CSV file (and cache it) from the URL and display a DataFrame. Add controls to do the following: - Control how many rows are displayed - A checkbox to determine to randomly shuffle the DataFrame Users select one or two variables and you must determine the best way to visualize them.
Drop files here
or click to upload
library(shiny)
library(ggplot2)
library(dplyr)
source("utils.R")
ui <- function(request) {
fluidPage(
titlePanel("Data Explorer"),
sidebarLayout(
sidebarPanel(
textInput("url", "Data URL:",
value = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"),
numericInput("n_rows", "Number of rows to display:",
value = 10, min = 1, step = 1),
checkboxInput("shuffle", "Randomly shuffle data", FALSE),
varSelectInput("var1", "Select first variable:", NULL),
varSelectInput("var2", "Select second variable (optional):",
NULL, multiple = FALSE),
actionButton("load_btn", "Load Data"),
bookmarkButton()
),
mainPanel(
tabsetPanel(
tabPanel("Data",
tableOutput("data_table")),
tabPanel("Plot",
plotOutput("plot"))
)
)
)
)
}
server <- function(input, output, session) {
# Cache for the loaded data
data_cache <- reactiveVal(NULL)
# Load and process data when button is clicked
observeEvent(input$load_btn, {
tryCatch({
data <- read.csv(input$url)
data_cache(data)
# Update variable selection inputs
updateVarSelectInput(session, "var1",
choices = names(data))
updateVarSelectInput(session, "var2",
choices = c("None" = "", names(data)))
}, error = function(e) {
showNotification(paste("Error loading data:", e$message),
type = "error")
})
})
# Prepare data for display
display_data <- reactive({
req(data_cache())
data <- data_cache()
if (input$shuffle) {
data <- data[sample(nrow(data)), ]
}
head(data, input$n_rows)
})
# Render data table
output$data_table <- renderTable({
display_data()
})
# Create appropriate visualization
output$plot <- renderPlot({
req(data_cache(), input$var1)
data <- data_cache()
create_visualization(data, input$var1, input$var2)
})
}
enableBookmarking("url")
shinyApp(ui = ui, server = server)
Hi! I can help you with any questions about Shiny and R. What would you like to know?