Created
July 9, 2018 01:38
-
-
Save markgrujic/7d9e30c9c7ed38f5fabc44cd3cebcc06 to your computer and use it in GitHub Desktop.
plots that flow??
This file contains 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) | |
# example data | |
df <- | |
as.data.frame(matrix(rnorm(250), nrow=25)) %>% | |
rownames_to_column(var = "Year") %>% | |
mutate(Year = as.numeric(Year)) | |
# Define UI for application that draws a histogram | |
ui <- fluidPage( | |
fluidRow( | |
column(width=8, selectInput('chart_list', 'Variables to plot', choices = names(df), multiple = T, width='100%')), | |
column(width=4, sliderInput('chart_height', 'Chart height', 100, 1000, 500)) | |
), | |
uiOutput('plots') | |
) | |
# Define server logic required to draw a histogram | |
server <- function(input, output) { | |
rv <- reactiveValues() | |
# when the selected variables change: | |
observeEvent(input$chart_list, { | |
req(input$chart_list) | |
rv$n_plots <- length(input$chart_list) | |
for(i in 1:rv$n_plots){ | |
local({ | |
ii <- i | |
# get each plot and save to output list | |
plotname <- paste("plot", ii, sep="") | |
output[[plotname]] <- renderPlot({ | |
ggplot(df, aes_string("Year", input$chart_list[ii])) + | |
geom_line() + | |
ggtitle(input$chart_list[ii]) | |
}) | |
}) | |
} | |
}) | |
output$plots <- renderUI({ | |
req(rv$n_plots) | |
if(rv$n_plots == 1){ | |
# just do a normal plotOutput for the selected plot | |
plotname <- paste("plot", 1, sep="") | |
plotOutput(plotname, height = paste(input$chart_height, "px", sep='')) | |
} else if(rv$n_plots > 1){ | |
# split the number of plots in two, create two columns and put in a fluidRow. | |
# get list of objects in each column (First col is 1, 3, 5, etc. Second is 2, 4, 6, etc.) | |
plot_output_list1 <- lapply(seq(1, rv$n_plots, by=2), function(ii) { | |
plotname <- paste("plot", ii, sep="") | |
plotOutput(plotname, height = paste(input$chart_height, "px", sep='')) | |
}) | |
plot_output_list2 <- lapply(seq(2, rv$n_plots, by=2), function(ii) { | |
plotname <- paste("plot", ii, sep="") | |
plotOutput(plotname, height = paste(input$chart_height, "px", sep='')) | |
}) | |
# get breaks for each plot in the lists | |
plot_output_list_break1 <- rep(list(br()), length(plot_output_list1)) | |
plot_output_list_break2 <- rep(list(br()), length(plot_output_list2)) | |
# get the combined list of plots and breaks by interleaving the lists | |
plot_output_list1 <- c(rbind(plot_output_list1, plot_output_list_break1)) | |
plot_output_list2 <- c(rbind(plot_output_list2, plot_output_list_break2)) | |
# plot the charts in their respective columns | |
fluidRow( | |
column(width=6, do.call(tagList, plot_output_list1)), | |
column(width=6, do.call(tagList, plot_output_list2)) | |
) | |
} | |
}) | |
} | |
# Run the application | |
shinyApp(ui = ui, server = server) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment