progArgs: Providing Arguments to a Script

progArgsR Documentation

Providing Arguments to a Script

Description

withArgs() allows you to \sourcelink an R script while providing arguments. As opposed to running with \Rscriptlink, the code will be evaluated in the same session in an environment of your choosing.

progArgs() is a generalized version of \codelink3basecommandArgs(trailingOnly = TRUE), allowing you to access the program's arguments whether it was \sourcenolink-ed or run from a shell.

asArgs() coerces R objects into a character vector, for use with command line applications and withArgs().

Usage

asArgs(...)
progArgs(ifnotfound = character())
withArgs(...)

Arguments

...

R objects to turn into script arguments; typically \codelink2baselogical, \codelink2basenumeric, \codelink2basecharacter, \codelink2base:DatesDate, and \codelink2base:DateTimeClassesPOSIXt vectors.

for withArgs(), the first argument should be an (unevaluated) call to a \sourcenolink-like function.

ifnotfound

any R object, e.g., character(0), NULL, a call to \codelink3basestop()

Details

progArgs() will return the arguments associated with the executing script, or commandArgs(trailingOnly = TRUE) when there is no executing script.

asArgs() coerces objects into command-line arguments. ... is first put into a list, and then each non-list element is converted to character. They are converted as follows:

Factors (class \stringfactor)

using \codelink3base:characteras.character.factor()

Date-Times (class \stringPOSIXct and \stringPOSIXlt)

using format "%Y-%m-%d %H:%M:%OS6" (retains as much precision as possible)

Numbers (class \stringnumeric and \stringcomplex)

with 17 significant digits (retains as much precision as possible) and \string. as the decimal point character.

Raw Bytes (class \stringraw)

using sprintf("0x%02x", ) (for easy conversion back to raw with \codelink3base:rawas.raw() or \codelink3base:vectoras.vector(, "raw"))

All others will be converted to character using \codelink3base:characteras.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 \stringNA (since the command-line arguments also never have missing strings).

Value

for asArgs() and progArgs(), a character vector.

for withArgs(), the result of evaluating the first argument.

Examples

@R_PACKAGE_NAME@::asArgs(NULL, c(TRUE, FALSE, NA), 1:5, pi, exp(6i),
    letters[1:5], as.raw(0:4), Sys.Date(), Sys.time(),
    list(list(list("lists are recursed"))))


FILE.R <- tempfile(fileext = ".R")
@R_PACKAGE_NAME@:::.writeCode({
    @R_PACKAGE_NAME@:::.withAutoprint({
        @R_PACKAGE_NAME@::sys.path()
        @R_PACKAGE_NAME@::progArgs()
    }, spaced = TRUE, verbose = FALSE, width.cutoff = 60L)
}, FILE.R)


## wrap your source call with a call to withArgs()
@R_PACKAGE_NAME@::withArgs(
    source(FILE.R, local = TRUE, verbose = FALSE),
    letters[6:10], pi, exp(1)
)
@R_PACKAGE_NAME@:::.Rscript(c("--default-packages=NULL", "--vanilla", FILE.R,
    @R_PACKAGE_NAME@::asArgs(letters[16:20], pi, Sys.time())))
@R_PACKAGE_NAME@:::.Rscript(c("--default-packages=NULL", "--vanilla",
    rbind("-e", readLines(FILE.R)[-2L]),
    @R_PACKAGE_NAME@::asArgs(letters[16:20], pi, Sys.time())))


# ## with R >= 4.1.0, use the forward pipe operator '|>' to
# ## make calls to withArgs() more intuitive:
# source(FILE.R, local = TRUE, verbose = FALSE) |> @R_PACKAGE_NAME@::withArgs(
#     letters[6:10], pi, exp(1))


## withArgs() also works with set.sys.path()
sourcelike <- function (file, envir = parent.frame())
{
    file <- set.sys.path(file)
    envir <- as.environment(envir)
    exprs <- parse(n = -1, file = file)
    for (i in seq_along(exprs)) eval(exprs[i], envir)
}
@R_PACKAGE_NAME@::withArgs(sourcelike(FILE.R), letters[21:26])


unlink(FILE.R)

ArcadeAntics/this.path documentation built on July 27, 2024, 12:05 a.m.