This worked for me. The png is generated by ggsave using predefined dimensions.
The figure is displayed in browser using the current session height and width. The figure resizes dynamically with browser
If the figure is saved, the original predefined dimensions are maintained.
#-----------------------------------------------------------------------------------
#Full working script shown
#ui.R
library(shiny)
library(reshape)
library(ggplot2)
shinyUI(pageWithSidebar(
headerPanel("ggplot figure test"),
sidebarPanel(),
mainPanel(imageOutput("figure")
)))
#server.R
library(datasets)
shinyServer(function(input, output,session) {
output$figure <- renderImage({
width <- session$clientData$output_figure_width
height <- session$clientData$output_figure_height
p2 <- ggplot(mtcars, aes(wt, mpg))+ geom_point()
outfile <- "image.png"
ggsave(outfile,p2,height=6, width=12,dpi=300)
list(src = outfile, contentType = 'image/png', width = width, height = width/2, alt = "image")
},deleteFile = TRUE)
})
#-----------------------------------------------------------------------------------