I'm trying to write an addin that will run the current R script in batch mode and create an output file with extension ".Rout". I've written a doRbatch
function that will run an R script in batch and that works fine. I just can't get it to run from an addin.
My doRbatch
function:
doRbatch <- function(progpath){
if (progpath == "") stop("No program path was specified")
if (!file.exists(progpath)) stop(paste("Program",progpath,"does not exist",sep="\n"))
system2("R",
args=c("CMD", "BATCH", "--no-save", "--no-restore",
shQuote(progpath))
)
}
This function works as intended. But if I try to call doRbatch
from an addin function, I get a message unable to open output file
and R continues to run. If I type something in the console, I get an error message Error: object 'n' not found
and R stops running.
My addin function:
doRbatchAddin <- function() {
doRbatch(rstudioapi::getSourceEditorContext()$path)
}
If I debug doRbatch
, I can see that progpath
is correct, the function just doesn't run.
Possibly related: when I install the package containing the addin, a separator line is inserted in the "Addins" list after the package name. Other packages (BOOKDOWN, CLIPR, COVR, DEVTOOLS, PKGDOWN) have a separator line before the package name.
Can anyone shed some light on why doRbatch
doesn't work when called from an addin?