rstudio / plumber

Turn your R code into a web API.
https://www.rplumber.io
Other
1.39k stars 256 forks source link

Graph plot size #107

Closed fusionet24 closed 6 years ago

fusionet24 commented 7 years ago

Hi,

I've used plumber to turn one of my functions into an api GET request where it returns png which is generated from a Grid extra grobb of ggplots.

The problem I'm having is how to set the output size of my png returned. It's returning it at 480 x 480px which is not useful and need to be dynamic depending on the amount of ggplots.

Where would I set these parameters? I've tried in gridextra and it doesn't seem to work. I tried Dev.off too.

Thanks.

datawookie commented 7 years ago

I've also run into this issue.

I dug through the code a bit and found that in the images.R file you have the following:

render_png <- list(
  pre = function(req, res, data){
    t <- tempfile()
    data$file <- t
    png(t)
  },
  post = function(value, req, res, data){
    dev.off()

    con <- file(data$file, "rb")
    img <- readBin(con, "raw", file.info(data$file)$size)
    close(con)
    res$body <- img
    res$setHeader("Content-type", "image/png")
    res
  }
)

In the pre() function there is a call to png(). This happens before the plot is rendered and writes the result of the plot to a PNG file. There are width and height arguments to png(), however, since neither of them is provided they assume the default, which is 480 px in both cases.

It would be great if we could pass through parameters which would specify height and width. Perhaps this could be done with a @serializer option before the API function definition?