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("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),
selectInput("var1", "Select first variable", choices = NULL),
selectInput("var2", "Select second variable (optional)",
choices = NULL, multiple = FALSE),
actionButton("update", "Update")
),
mainPanel(
tabsetPanel(
tabPanel("Data", dataTableOutput("data_table")),
tabPanel("Visualization",
plotOutput("viz_plot"),
verbatimTextOutput("viz_info"))
)
)
)
)
server <- function(input, output, session) {
# Reactive value to store the data
data <- reactiveVal(NULL)
# Initial data load
observe({
tryCatch({
df <- read_and_cache_csv(input$url)
data(df)
# Update variable choices
updateSelectInput(session, "var1",
choices = c("", colnames(df)),
selected = "")
updateSelectInput(session, "var2",
choices = c("", colnames(df)),
selected = "")
}, error = function(e) {
showNotification(paste("Error loading data:", e$message),
type = "error")
})
})
# Update when button is clicked
observeEvent(input$update, {
tryCatch({
df <- read_and_cache_csv(input$url)
data(df)
# Update variable choices
updateSelectInput(session, "var1",
choices = c("", colnames(df)),
selected = "")
updateSelectInput(session, "var2",
choices = c("", colnames(df)),
selected = "")
}, error = function(e) {
showNotification(paste("Error loading data:", e$message),
type = "error")
})
})
# Display data table
output$data_table <- renderDataTable({
req(data())
df <- data()
if (input$shuffle) {
df <- df[sample(nrow(df)), ]
}
head(df, input$n_rows)
})
# Create visualization
output$viz_plot <- renderPlot({
req(data(), input$var1)
df <- data()
if (input$shuffle) {
df <- df[sample(nrow(df)), ]
}
create_visualization(df, input$var1, input$var2)
})
# Display visualization info
output$viz_info <- renderText({
req(data(), input$var1)
df <- data()
get_viz_info(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?