Passing ggplo2 plot to htmlwidget



I am trying to write an htmlwidget which is supposed to take a ggplot2 plot as input from the user; this plot should then be displayed in a div along with other information.

The problem I have is how to pass a ggplot2 plot to the widget, i.e. to the js side.
Currently I save the plot as a png to disk and then serialize it to base64 format string; this string is then passed to the js by htmlwidgets and can then be included as the src attribute in an img tag.

However I don't quite like this solution as it is kind of slow for an interactive analysis. Ideally, I would like to be able to source the plot as a png in the img html tag in the same way as when the plot is printed on the console (i.e. it appears on the rstudio pane) or in r markdown.

Any idea about how I can achieve that?