3

Provided I have a saved image file, how do I serve them through plumber?

For instance, this works without issues

# save this as testPlumb.R

library(magrittr)
library(ggplot2)

#* @get /test1
#* @png
test1 = function(){
    p = data.frame(x=1,y= 1) %>% ggplot(aes(x=x,y=y)) + geom_point()
    print(p)
}

and run

plum = plumber::plumb('testPlumb.R')
plum$run(port=8000)

and if you go to http://localhost:8000/test1, you'll see the plot being served.

But I was not able to find a way to serve an image file

#* @get /test2
#* @png
test2 = function(){
    p = data.frame(x=1,y= 1) %>% ggplot(aes(x=x,y=y)) + geom_point()
    ggsave('file.png',p)

    # code that modifies that file a little that doesn't matter here

    # code that'll help me serve the file
}

In place of code that'll help me serve the file above, I have tried include_file as suggested here but that failed.

Since the code that modifies that file a little that doesn't matter here part is using the magick package, I have also tried serving magick objects using print but that also have not been successful.

For instance

#* @get /test3
#* @png
test3 = function(){
    p = data.frame(x=1,y= 1) %>% ggplot(aes(x=x,y=y)) + geom_point()
    ggsave('file.png',p)
    fig = image_read(file)
    fig = image_trim(fig)
    print(fig) # or just fig
}

results in {"error":["500 - Internal server error"],"message":["Error in file(data$file, \"rb\"): cannot open the connection\n"]}

OganM
  • 2,543
  • 16
  • 33
  • Parts of this look like R but the rest looks like gibberish to this R programmer. In R one would need to load the ggplot2 package. Is that something that your API does automagically? – IRTFM Apr 26 '18 at 02:22
  • Could you clarify which part is unclear? – OganM Apr 26 '18 at 02:26
  • You've now added library calls, but I'm guessing that these are actually files that are accessed (somehow) via some function or another in a package named `plumber`? (Since the octothorpe would normally prevent any evaluation of the first couple of lines.) Wouldn't there need to also be a library call to ignore plumber if this were being done in R? – IRTFM Apr 26 '18 at 02:29
  • added some more clarifications. those pounded lines are interpreted by plumber. some documentation about them can be found [here](https://www.rplumber.io/docs/index.html) but it is a little sparse – OganM Apr 26 '18 at 02:38
  • Great. Now you set up some kind of non-exiting loop in my R console session. Forcing the process to quit. (I did see the plot "served" in my browser.) – IRTFM Apr 26 '18 at 02:44
  • Well, that's how plumber works. Reads, file, starts listening for the port and responding until you tell it not to – OganM Apr 26 '18 at 02:47
  • Ok problem solved. seems like I needed to bypass the default serializer as described [here](https://www.rplumber.io/docs/rendering-and-output.html#bypassing-serialization) – OganM Apr 26 '18 at 02:57

1 Answers1

11

As described here one needs to bypass the default png serializer to make this work. So replacing #* @png with #* @serializer contentType list(type='image/png') and reading the file via readBin at the end solves the issue

#* @get /test2
#* @serializer contentType list(type='image/png')
test2 = function(){
    p = data.frame(x=1,y= 1) %>% ggplot(aes(x=x,y=y)) + geom_point()
    file = 'file.png'
    ggsave(file,p)

    readBin(file,'raw',n = file.info(file)$size)
}
Holger Brandl
  • 10,634
  • 3
  • 64
  • 63
OganM
  • 2,543
  • 16
  • 33
  • Did `readBin` render the image as a response? I'm trying to do this, and my UI/UX engineer gets a 404 error when hitting the API. Also to note, our `.png` files were not generated from any graphing functions. Could that have something to do with my troubles? – Vince May 15 '19 at 14:22
  • Sorry missed this. Source of the plot shouldn't matter, your get request should simply return an image. [This](http://oganm.com/api/text2img?t=Text) endpoint here returns it's output using [this](https://github.com/oganm/serverAPI/blob/5e2f1ede4ec76d4fa4822309ad096bac745ecf20/server.R#L25-L38) code. 404 sounds more like a set up issue – OganM Aug 22 '19 at 23:09