質問

I'm trying to capture a multi-plot pdf from a function. In R, this gives me a three page PDF:

pdf(file='test.pdf', onefile=TRUE)
lapply(1:3, 'plot')
dev.off()

Using OpenCPU:

$ curl http://localhost:6977/ocpu/library/base/R/lapply -H 'Content-Type: application/json' -d '{"X":[1,2,3], "FUN":"plot"}'
/ocpu/tmp/x0dc3dad0/R/.val
/ocpu/tmp/x0dc3dad0/graphics/1
/ocpu/tmp/x0dc3dad0/graphics/2
/ocpu/tmp/x0dc3dad0/graphics/3
/ocpu/tmp/x0dc3dad0/stdout
/ocpu/tmp/x0dc3dad0/source
/ocpu/tmp/x0dc3dad0/console
/ocpu/tmp/x0dc3dad0/info

I can get any of the individual pages as a single-page PDF file, but not as one combined file.

Two possible work-arounds, not without their share of problems:

  1. Use par(mfrow), layout(), or a similar mechanism, though this will create a monster image in the end (I'm dealing with more than three images in my code).

  2. Use tempfile, create an Rmd file on-the-fly, return the filename in the session (have not tested this yet), and use OpenCPU's processing of Rmd files. Unfortunately, this now uses LaTeX's geometries and page numbering (workarounds exist for this).

Are there other ways to do this?

役に立ちましたか?

解決

Good question. OpenCPU captures graphics using evaluate which stores each graphic individually. The API itself doesn't support combining multiple graphics within a single file. I would personally do this sort of PDF post processing in the application layer (i.e. with non-R tools), but perhaps it would be useful to support this in the API.

Some suggestions:

  • Any file that your R function/script saves to the working directory (i.e. getwd()) will also become available through the API. So one thing you could do is in your R code manually create your combined pdf file and save it to the working directory and then download it through opencpu.

  • Graphics are actually recordedPlot objects, and besides png, pdf and svg, you can also retrieve the graphic as rds or rda. So you could write an R function that downloads the recordedPlot object from the API and then prints it. Not sure if that would be helpful in your use case.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top