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"]}