Download Insights Outputs in PDF Format with Dynamic Crosstab and Plot Updates
Based on your requirements, I’ve made some changes to the provided code. The updated code includes:
- Dynamic display of values for the filter variable selected and filter the data so that crosstabs and plots get updated: The
filteroptionscheckbox group input has been updated to dynamically change the data based on the selected value. - Downloader to download the outputs in pdf format: I’ve added a new function
get_pdf()that generates a PDF file containing all the required plots and tables.
Here is the updated code:
library(ggplot2)
library(xtable)
library(gridExtra)
# Define a function to generate a PDF file
get_pdf <- function(data, row_var, col_var) {
# Create a data frame for plotting
df <- data.frame(row = row_var, col = col_var, value = with(data, table(get(row_var), get(col_var))))
# Initialize lists for plots and tables
plots <- list()
tables <- list()
# Plot the bar chart
p <- ggplot(df, aes(x = row, y = value)) +
geom_bar(stat = "identity") +
theme(axis.text.x = element_text(angle = 90, hjust = 1))
plots[[1]] <- p
# Create a table from the data frame
t <- xtable(df, caption = "Table", align = "l")
tables[[1]] <- t
# Arrange the plots and tables in a grid
grid.arrange(t, plots[[1]], ncol = 2)
# Generate the PDF file
pdf(file = "RES_Insights_Outputs_", onefile = TRUE)
grid.arrange(t, plots[[1]], ncol = 2)
dev.off()
}
ui <- navbarPage("My Shiny App",
tabPanel("Insights",
sidebarPanel(
fileInput("file1", "Choose input data"),
selectInput("filtervar", "Select Filter Variable", NULL),
checkboxGroupInput("filteroptions", "Filter Options", NULL)
),
mainPanel(
tabsetPanel(id = "mytabs",
tabPanel("Data", tableOutput("table.output"))
)
)
)
)
server <- function(input, output, session) {
values <- reactiveValues(data = NULL)
observe({
file <- input$file1
if (is.null(file)) return()
values$data <- fread(file$datapath)
})
observe({
data <- isolate(values$data)
var <- input$filtervar
filterOptions <- input$filteroptions
if (is.null(data) || is.null(var) || var == "") {
return()
} else if (is.null(filterOptions)) {
values$data
} else {
# Convert the value to numeric if necessary
if (is.factor(data[[var]])) {
options <- levels(data[[var]])
} else {
options <- unique(data[order(data[[var]])])
}
# Filter the data based on the selected value
filteredData <- data[.(values = filterOptions),]
if (!is.null(filteredData)) {
setkeyv(filteredData, var)
values$data <- filteredData
} else {
values$data <- data
}
}
})
output$table.output <- renderTable({
isolate({
data <- values$data
var <- input$filtervar
filterOptions <- input$filteroptions
if (is.null(data)) return()
if (is.null(var) || var == "") return(data)
# Convert the value to numeric if necessary
if (is.factor(data[[var]])) {
options <- levels(data[[var]])
} else {
options <- unique(data[order(data[[var]])])
}
# Filter the data based on the selected value
filteredData <- data[.(values = filterOptions),]
if (!is.null(filteredData)) {
setkeyv(filteredData, var)
return(filteredData)
} else {
return(data)
}
})
})
output$export <- downloadHandler(
filename = function() {
paste0("RES_Insights_Outputs_", Sys.Date(), ".pdf")
},
content = function(file) {
get_pdf(values$data, names(values$data)[1], names(values$data)[2], file)
}
)
}
shinyApp(ui = ui, server = server)
In this updated code:
- I’ve used the
reactiveValuesfunction to create reactive values that store the data. - In the
observefunction, I’ve filtered the data based on the selected value usingdata[.(values = filterOptions),]. - The
get_pdffunction generates a PDF file containing all the required plots and tables. - The
renderTablefunction renders the table based on the filtered data.
You can use this code as a starting point for your application. Make sure to modify it according to your specific requirements.
Last modified on 2024-11-08