Args {this.path} | R Documentation |
withArgs
allows you source
R code while providing
arguments. This would be in the circumstance that you want to run a script and
provide command-line arguments, but want the objects to appear in your
environment, or if you just don't want to shQuote
the
arguments.
fileArgs
is a generalized version of commandArgs
,
allowing you to access the script's arguments whether it was sourced or run
from a shell.
asArgs
coerces R objects into a character vector, for use with command
line applications and withArgs
.
from.shell
tells you if the R script was run from a shell.
asArgs(...)
fileArgs()
withArgs(expr, ...)
from.shell()
... |
R objects to turn into scripts arguments; typically
|
expr |
an (unevaluated) call to |
...
is first put into a list, and then each non-list element is
converted to character. They are converted as follows:
"factor"
)using as.character.factor
"POSIXct"
and "POSIXlt"
)using format "%Y-%m-%d %H:%M:%OS6"
(retains as much precision as
possible)
"numeric"
and "complex"
)with 17 significant digits (retains as much precision as possible) and
"."
as the decimal point character.
"raw"
)using sprintf("0x%02x", )
(can easily convert back to raw
with as.raw()
or
as.vector(, "raw")
)
All others will be converted to character using
as.character
and its methods.
The arguments will then be unlisted, and all attributes will be removed.
Arguments that are NA_character_
after conversion will be converted to
"NA"
(since the command-line arguments also never have missing strings).
Consider that it may be better to use Rscript
combined with
save
and load
or
saveRDS
and readRDS
.
Also consider that what you want is a function, and not a script. If you're already at the R level, it is easier and more flexible to source a script that creates a function, and then use that function. This only applies if the script you are sourcing will never be run from the command-line, or at least will never be run on its own (it will only be used in the context of other scripts, it will NEVER be used as a stand alone script).
for asArgs
and fileArgs
, a character vector.
for withArgs
, the result of evaluating expr
.
asArgs(
NULL,
c(TRUE, FALSE, NA),
1:5,
pi,
exp(6i),
letters[1:5],
as.raw(0:4),
as.Date("1970-01-01"),
as.POSIXct("1970-01-01 00:00:00"),
list(
list(
list(
"lists are recursed"
)
)
)
)
this.path:::write.code(file = FILE <- tempfile(), {
withAutoprint({
this.path()
fileArgs()
from.shell()
}, verbose = FALSE)
})
# wrap your source call with a call to 'withArgs'
withArgs(
source(FILE, local = TRUE, verbose = FALSE),
letters, pi, exp(1)
)
withArgs(
sys.source(FILE, environment()),
letters, pi + 1i * exp(1)
)
this.path:::.Rscript(c("--default-packages=this.path", "--vanilla", FILE,
asArgs(letters, pi, as.POSIXct("2022-07-17 04:25"))))
# with R >= 4.1.0, use the forward pipe operator '|>' to
# make calls to 'withArgs' more intuitive:
# source(FILE, local = TRUE, verbose = FALSE) |> withArgs(
# letters, pi, exp(1)
# )
# sys.source(FILE, environment()) |> withArgs(
# letters, pi + 1i * exp(1)
# )