Skip to content

Instantly share code, notes, and snippets.

@mayank7jan
Created June 26, 2021 17:10
Show Gist options
  • Save mayank7jan/0e89c1d6f48379881da607140f45b2ac to your computer and use it in GitHub Desktop.
Save mayank7jan/0e89c1d6f48379881da607140f45b2ac to your computer and use it in GitHub Desktop.
Shiny example app with dynamic number of plots with plot column width and auto align
library(shiny)
library(ggplot2)
ui <- shinyUI(fluidPage(
navbarPage(
windowTitle = "Automated EDA",
title = "Automated EDA",
collapsible = TRUE,
tabPanel(title = "EDA",
icon = icon("edit"),
fluidRow(column(
width = 12,
uiOutput('univariate_plots')
)))
)
))
server <- function(input, output,session) {
rv <- reactiveValues()
observe({
isolate({
rv$data_filtered <- datasets::iris
})
})
observeEvent(rv$data_filtered, {
uniPlot <- lapply(names(rv$data_filtered), function(col_i){
ggplot(rv$data_filtered, aes_string(x=col_i)) + geom_bar(fill = "#8cd3ff") + theme_classic() + labs(title = col_i) + xlab(col_i) + ylab("Count")
})
output$univariate_plots <- renderUI({
plot_output_list <- lapply(seq_along(1:length(uniPlot)),function(plot_i){
column(width = 6,
tags$div(style = "margin-top: 10px; margin-bottom: 10px;",
plotOutput(outputId = paste0("uni_", plot_i))
))
})
# Convert the list to a tagList - this is necessary for the list of items to display properly.
do.call(tagList, plot_output_list)
## either works
# plot_output_list
})
rv$uniPlot <- uniPlot
})
observeEvent(rv$uniPlot,{
lapply(seq_along(1:length(rv$uniPlot)), function(plot_i) {
output[[paste0("uni_", plot_i)]] <- renderPlot({
rv$uniPlot[[plot_i]]
})
})
}, ignoreInit = FALSE)
}
shinyApp(ui, server)
@mayank7jan
Copy link
Author

App Output Preview :

image

@mayank7jan
Copy link
Author

Future update - Show R code for each plot (visible on click of accordion panel)

@mayank7jan
Copy link
Author

On click of each plot - make it full screen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment