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(dplyr)
library(ggplot2)
source("utils.R")
ui <- fluidPage(
titlePanel("URL Data Explorer"),
sidebarLayout(
sidebarPanel(
textInput("url", "Enter CSV URL:",
value = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/penguins.csv"),
numericInput("n_rows", "Number of rows to display:", value = 10, min = 1),
checkboxInput("shuffle", "Randomly shuffle data", value = FALSE),
selectInput("var1", "Select first variable:", choices = NULL),
selectInput("var2", "Select second variable (optional):", choices = NULL, selected = NULL),
actionButton("update", "Update Data")
),
mainPanel(
tabsetPanel(
tabPanel("Data", dataTableOutput("data_table")),
tabPanel("Visualization", plotOutput("plot"))
)
)
)
)
server <- function(input, output, session) {
# Reactive value to store the data
data <- reactiveVal()
# Function to read and process data
observeEvent(input$update, {
tryCatch({
df <- read_and_cache_csv(input$url)
data(df)
# Update variable choices
updateSelectInput(session, "var1",
choices = c("", colnames(df)))
updateSelectInput(session, "var2",
choices = c("", colnames(df)))
}, error = function(e) {
showNotification(paste("Error loading data:", e$message),
type = "error")
})
})
# Reactive expression for displayed data
displayed_data <- reactive({
req(data())
df <- data()
if (input$shuffle) {
df <- df[sample(nrow(df)), ]
}
df
})
# Display data table
output$data_table <- renderDataTable({
head(displayed_data(), input$n_rows)
})
# Create visualization
output$plot <- renderPlot({
req(displayed_data(), input$var1)
df <- displayed_data()
if (input$var1 == "") return(NULL)
if (input$var2 == "") {
# Single variable visualization
create_univariate_plot(df, input$var1)
} else {
# Two variable visualization
create_bivariate_plot(df, input$var1, input$var2)
}
})
# Initialize data on startup
observeEvent(1, {
click("update")
}, once = TRUE)
}
shinyApp(ui = ui, server = server)
Hi! I can help you with any questions about Shiny and R. What would you like to know?