Skip to content

Instantly share code, notes, and snippets.

@nanxstats
Last active January 11, 2023 16:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanxstats/5b5e5754d31d44d515f173d553039466 to your computer and use it in GitHub Desktop.
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
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