Reputation: 44638
Is there an elegant way to run a script contained in an installed package from the command line, with arguments.
So, I'm aware that we could do something like:
Rscript path/package/scritpts/script.R arg1 arg2 arg3
But I'd like something that's more elegant. Because path/package
can be ridiculous sometimes.
Upvotes: 0
Views: 52
Reputation: 44638
Dirk has provided a viable solution by way of his littler
package:
Rscript $(R -e 'cat(system.file("scripts","file.R",package = "pkg"))') arg1 arg2 arg3
Upvotes: 0
Reputation: 160407
Not much of an answer, but a naïve example using system.file
and such:
# Rscript --vanilla pkgscript.R packagname scriptname.R arg1 arg2 arg3
args <- commandArgs(trailingOnly = TRUE)
if (length(args) < 2L) {
# print out "Usage:" stuff
stop("Usage: ...")
} else {
fnames <- list.files(path = system.file(package = args[1]), pattern = args[2],
recursive = TRUE, full.names = TRUE)
lfn <- length(fnames)
if (lfn == 0L) {
stop("no script found for: ", sQuote(args[2]))
} else if (lfn == 1L) {
# do something with args[-(1:2)]
source(fnames)
} else {
stop("multiple scripts found for: ", sQuote(args[2]))
}
}
Upvotes: 1