Skip to content
Prev 10826 / 12125 Next

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

On Sun, 28 Jul 2024 15:27:33 -0500
Dirk Eddelbuettel <edd at debian.org> wrote:

            
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