[R-pkg-devel] R CMD BATCH plot output

Dirk Eddelbuettel edd @end|ng |rom deb|@n@org
Tue Jul 30 00:18:25 CEST 2024


On 29 July 2024 at 13:02, Ivan Krylov wrote:
| On Sun, 28 Jul 2024 15:27:33 -0500
| Dirk Eddelbuettel <edd using debian.org> wrote:
| 
| > If we cannot (or do not want to) modify the given main.R, I would
| > suggest something along the lines of
| > 
| >   Rscript -e 'pdf(myfilenamevar); source("main.R"); dev.off()' | tee
| > 2024-07-28.log
| 
| Perhaps even options(device = \(file = myfilenamevar, ...) pdf(file =
| file, ...)) so that every plot would get the same treatment, though
| that requires re-implementing the dev.new() logic to guess an available
| file name. You can even misuse R_PROFILE_USER to inject the code into
| the R CMD BATCH session:
| 
| # myplots.R:
| local({
| 	cmdline <- commandArgs(FALSE)
| 	srcfile <- cmdline[[which(cmdline == '-f')[[1]] + 1]]
| 	plotfile <- sub('(\\.R)?$', '', srcfile)
| 	options(device = \(file, ...) {
| 		i <- 1
| 		if (missing(file)) repeat {
| 			file <- sprintf('%s_%03d.pdf', plotfile, i)
| 			if (!file.exists(file)) break
| 			i <- i+1
| 		}
| 		pdf(file = file, ...)
| 	})
| })
| 
| # example.R:
| plot(1:100 / 10, sin(1:100 / 10))
| dev.off()
| plot(1:100 / 10, cos(1:100 / 10))
| 
| R_PROFILE_USER=myplots.R R CMD BATCH example.R
| # produces example.Rout, example_001.pdf, example_002.pdf

Impressive. A very creative way to inject a modification into the (to my
taste overly restrictive) setup provided by R CMD BATCH.

Personally I would much rather script something cleaner with r or Rscript but
we all have our preferences.

Dirk


-- 
dirk.eddelbuettel.com | @eddelbuettel | edd using debian.org



More information about the R-package-devel mailing list