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(tidyverse)
library(ggplot2)
source("utils.R")
ui <- fluidPage(
titlePanel("Dynamic Data Visualization"),
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:",
min = 1, max = 1000, value = 10),
checkboxInput("shuffle", "Randomly shuffle data", FALSE),
selectInput("var1", "Select first variable:", choices = NULL),
selectInput("var2", "Select second variable (optional):",
choices = NULL, selected = NULL),
actionButton("update", "Update Data")
),
mainPanel(
h4("Data Preview:"),
tableOutput("data_preview"),
h4("Visualization:"),
plotOutput("plot")
)
)
)
server <- function(input, output, session) {
# Reactive data fetch and processing
data <- eventReactive(input$update, {
fetch_and_process_data(input$url)
}, ignoreNULL = FALSE)
# Update variable selection choices when data changes
observe({
df <- data()
if (!is.null(df)) {
updateSelectInput(session, "var1",
choices = c("None" = "", colnames(df)))
updateSelectInput(session, "var2",
choices = c("None" = "", colnames(df)))
}
})
# Prepare data for display
display_data <- reactive({
df <- data()
if (is.null(df)) return(NULL)
if (input$shuffle) {
df <- df[sample(nrow(df)), ]
}
head(df, input$n_rows)
})
# Data preview output
output$data_preview <- renderTable({
display_data()
})
# Plot output
output$plot <- renderPlot({
df <- data()
if (is.null(df)) return(NULL)
if (input$var1 == "") return(NULL)
create_visualization(df, input$var1, input$var2)
})
}
shinyApp(ui = ui, server = server)
Hi! I can help you with any questions about Shiny and R. What would you like to know?