On Sun, 28 Jul 2024 15:27:33 -0500
Dirk Eddelbuettel <e...@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

-- 
Best regards,
Ivan

______________________________________________
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

Reply via email to