library(shiny)
## Warning: package 'shiny' was built under R version 4.4.2
library(ggplot2)
# Creates a sample dataset directly in the script
data <- data.frame(
date = rep(seq.Date(from = as.Date("2023-01-01"), by = "month", length.out = 12), 3),
product = rep(c("Product A", "Product B", "Product C"), each = 12),
revenue = c(
runif(12, min = 5000, max = 15000),
runif(12, min = 7000, max = 20000),
runif(12, min = 3000, max = 12000)
)
)
# UI
ui <- fluidPage(
titlePanel("Shiny Demo"),
sidebarLayout(
sidebarPanel(
selectInput("selectedProduct", "Select Product:",
choices = unique(data$product), selected = "Product A")
),
mainPanel(
plotOutput("trendPlot"),
plotOutput("revenuePlot")
)
)
)
# Server
server <- function(input, output) {
# Render Trend Plot
output$trendPlot <- renderPlot({
filtered_data <- subset(data, product == input$selectedProduct)
ggplot(filtered_data, aes(x = date, y = revenue)) +
geom_line(color = "blue", size = 1) +
labs(title = paste("Revenue Trends for", input$selectedProduct),
x = "Date", y = "Revenue") +
theme_minimal()
})
# Render Total Revenue Plot
output$revenuePlot <- renderPlot({
ggplot(data, aes(x = product, y = revenue, fill = product)) +
geom_bar(stat = "identity") +
labs(title = "Total Revenue by Product", x = "Product", y = "Total Revenue") +
theme_minimal()
})
}
# Combine UI and Server
shinyApp(ui = ui, server = server)
Shiny applications not supported in static R Markdown documents