Shiny Application for Interactive Data Visualization and Summarization
The code you provided is a Shiny application that creates an interactive dashboard for visualizing and summarizing data. Here’s a breakdown of the main components:
- Data Import: The application allows users to upload a CSV file containing the data. The
read.csvfunction reads the uploaded file and stores it in a reactive expressiondat. - Period Selection: Users can select a period from the data using a dropdown menu. This selection is stored in a reactive expression
input$period. - Time Point Selection: Users can select a specific point-in-time from the selected period using another dropdown menu. This selection is stored in a reactive expression
input$time. - Filtering Variable Selection: Users can select a filtering variable (e.g., “Group”) from a list of available variables. This selection is stored in a reactive expression
input$filter_var. - Filter Values Selection: If a filtering variable is selected, users can choose specific values to filter the data. This selection is stored in a reactive expression
input$filter_vals. - Summarized Variable Selection: Users can select a variable to summarize (e.g., “Total Sales”). This selection is stored in a reactive expression
input$vals. - Data Visualization: The application displays two tables: one showing the raw data and another showing the summarized data.
Here’s an updated version of your response with some formatting improvements:
Shiny Application
The Shiny application creates an interactive dashboard for visualizing and summarizing data.
library(shiny)
library(dplyr)
# Define UI
ui <- fluidPage(
# Data Import
fileInput("file1", "Select CSV file"),
# Period Selection
selectInput("period", "Period:", choices = names(dat())),
# Time Point Selection
selectInput("time", "Time point:", choices = unique(na.omit(dat()[[input$period]]))),
# Filtering Variable Selection
selectizeInput("filter_var", "Filtering variable:", choices = names(dat()), selected=""),
# Filter Values Selection
selectizeInput("filter_vals", "Filter values:", choices = c("", unique(na.omit(dat()[[input$filter_var]]))), multiple = TRUE),
# Summarized Variable Selection
selectInput("vals", "Variable to be summarized:", choices = names(dat()), selected = names(dat())[ncol(dat())]),
# Data Visualization
tableOutput("data"),
tableOutput("summed_data")
)
# Define server function
server <- function(input, output) {
# Initialize reactive expressions
dat <- reactive({
req(input$file1)
read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
})
# Period Selection
period <- reactive({
pds <- names(dat()) %>% select(contains("Period")) %>% names
chc_pd <- pds
names(chc_pd) <- paste0("By ", gsub("_", "", pds))
selectInput(inputId = "period",
label = NULL,
choices = chc_pd,
selected = pds[1])
})
# Time Point Selection
time <- reactive({
req(dat())
req(input$period)
chc <- unique(na.omit(dat()[[input$period]]))
selectInput(inputId = "time",
label = "Choose a point-in-time:",
choices = chc,
selected = chc[1])
})
# Filtering Variable Selection
filter_var <- reactive({
req(dat())
if(input$filter_var != ""){
chc_filt <- names(dat()) %>% select(contains("Filtering variable"))
selectizeInput("filter_var",
label = "Filtering variable:",
choices = c("", names(dat())),
selected="")
}
})
# Filter Values Selection
filter_vals <- reactive({
req(dat())
if(input$filter_var != ""){
chc_fv <- unique(na.omit(dat()[[input$filter_var]]))
selectizeInput("filter_vals",
label = "Filter values:",
choices = c("", chc_fv),
selected="",
multiple=TRUE)
}
})
# Summarized Variable Selection
vals <- reactive({
req(dat())
if(input$vals != ""){
selectInput(inputId = "vals",
label = "Variable to be summarized:",
choices = names(dat()),
selected = names(dat())[ncol(dat())])
}
})
# Data Visualization
output$data <- renderTable({
req(dat())
qs <- ifelse(is.character(dat()[[input$period]]), "'", "")
filter_exp1 <- paste0(input$period, "=='", input$time)
dat_filtered <- dat() %>% filter(eval(filter_exp1), eval(quote(input$filter_var == input$filter_vals)), eval(quote(input$vals == names(dat()))))
})
output$summed_data <- renderTable({
req(dat())
qs <- ifelse(is.character(dat()[[input$period]]), "'", "")
filter_exp1 <- paste0(input$period, "=='", input$time)
dat_filtered <- dat() %>% filter(eval(filter_exp1), eval(quote(input$filter_var == input$filter_vals)), eval(quote(input$vals == names(dat()))))
summarise_dat <- dat_filtered %>% summarise(mean(value))
})
}
# Run the app
shinyApp(ui = ui, server = server)
This updated version includes comments and formatting to make it easier to understand. Note that this code assumes you have dplyr installed in your R environment. If not, install it using install.packages("dplyr").
Last modified on 2023-06-22