this.path::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"
)
)
)
)
FILE <- tempfile(fileext = ".R")
this.path:::write.code({
withAutoprint({
this.path::this.path()
this.path::fileArgs()
this.path::progArgs()
}, spaced = TRUE, verbose = FALSE, width.cutoff = 60L)
}, FILE)
# wrap your source call with a call to 'withArgs'
this.path::withArgs(
source(FILE, local = TRUE, verbose = FALSE),
letters, pi, exp(1)
)
this.path::withArgs(
sys.source(FILE, environment()),
letters, pi + 1i * exp(1)
)
this.path:::.Rscript(c("--default-packages=this.path", "--vanilla", FILE,
this.path::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) |> this.path::withArgs(
# letters, pi, exp(1)
# )
# sys.source(FILE, environment()) |> this.path::withArgs(
# letters, pi + 1i * exp(1)
# )
# withArgs() also works with inside.source() and wrap.source()
sourcelike <- function (file, envir = parent.frame())
{
file <- inside.source(file)
envir <- as.environment(envir)
exprs <- parse(n = -1, file = file, srcfile = NULL, keep.source = FALSE)
for (i in seq_along(exprs)) eval(exprs[i], envir)
}
this.path::withArgs(sourcelike(FILE), letters)
sourcelike2 <- function (file, envir = parent.frame())
{
envir <- as.environment(envir)
exprs <- parse(n = -1, file = file, srcfile = NULL, keep.source = FALSE)
for (i in seq_along(exprs)) eval(exprs[i], envir)
}
sourcelike3 <- function (file, envir = parent.frame())
{
envir <- as.environment(envir)
wrap.source(sourcelike2(file = file, envir = envir))
}
this.path::withArgs(sourcelike3(FILE), letters)
this.path::withArgs(wrap.source(sourcelike2(FILE)), letters)
unlink(FILE)
Run the code above in your browser using DataLab