Created
June 26, 2021 17:10
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On click of each plot - make it full screen