Last active
January 11, 2023 16:24
-
-
Save nanxstats/5b5e5754d31d44d515f173d553039466 to your computer and use it in GitHub Desktop.
ggplot2 responsive panels with shiny and facet_wrap() https://d.cosx.org/d/423852
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") | |
library("ggplot2") | |
ui <- fluidPage( | |
plotOutput("plot_responsive") | |
) | |
server <- function(input, output, session) { | |
output$plot_responsive <- renderImage( | |
{ | |
width <- session$clientData$output_plot_responsive_width | |
height <- session$clientData$output_plot_responsive_height | |
outfile <- tempfile(fileext = ".png") | |
png(outfile, width = width * 3, height = height * 3, res = 300) | |
p <- ggplot(mpg, aes(displ, hwy)) + geom_point() | |
i <- findInterval(width, c(540, 720, 960, 1140, 1320)) + 1 | |
p <- p + facet_wrap(vars(class), ncol = c(1, 2, 3, 4, 6, 8)[i]) | |
print(p) | |
dev.off() | |
list( | |
src = outfile, | |
width = width, | |
height = height | |
) | |
}, | |
deleteFile = TRUE | |
) | |
} | |
shinyApp(ui, server) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment