FILE1.R <- tempfile(fileext = ".R")
this.path:::.write.code({
this.path:::.withAutoprint({
cat(sQuote(this.path::sys.path(verbose = TRUE, default = {
stop("since the executing script's path will be found,\n",
" 'default' will not be evaluated, so this error\n",
" will not be thrown! use this to your advantage to\n",
" do arbitrary things only if the executing script\n",
" does not exist")
})), "\n\n")
}, spaced = TRUE, verbose = FALSE, width.cutoff = 60L,
prompt = Sys.getenv("R_PROMPT"), continue = Sys.getenv("R_CONTINUE"))
}, FILE1.R)
oenv <- this.path:::.envvars(R_PROMPT = "FILE1.R> ",
R_CONTINUE = "FILE1.R+ ")
## 'sys.path()' works with 'source()'
source(FILE1.R, verbose = FALSE)
## 'sys.path()' works with 'sys.source()'
sys.source(FILE1.R, envir = environment())
## 'sys.path()' works with 'debugSource()' in 'RStudio'
if (.Platform$GUI == "RStudio")
get("debugSource", "tools:rstudio", inherits = FALSE)(FILE1.R)
## 'sys.path()' works with 'testthat::source_file()'
if (requireNamespace("testthat"))
testthat::source_file(FILE1.R, chdir = FALSE, wrap = FALSE)
## 'sys.path()' works with 'knitr::knit()'
if (requireNamespace("knitr")) {
FILE2.Rmd <- tempfile(fileext = ".Rmd")
FILE3.md <- tempfile(fileext = ".md")
writeLines(c(
"```{r}",
## same expression as above
deparse(parse(FILE1.R)[[c(1L, 2L, 2L)]]),
"```"
), FILE2.Rmd)
# knitr::knit(FILE2.Rmd, output = FILE3.md, quiet = FALSE)
## the above does not work when using the 'Run examples' button in
## the HTML documentation. 'package:knitr' cannot knit a document
## inside another document, pretty embarrassing oversight, so we
## have to launch a new R session and knit the document from there
FILE4.R <- tempfile(fileext = ".R")
this.path:::.write.code(bquote({
knitr::knit(.(FILE2.Rmd), output = .(FILE3.md), quiet = TRUE)
}), FILE4.R)
this.path:::.Rscript(
c("--default-packages=NULL", "--vanilla", FILE4.R),
show.command = FALSE
)
unlink(FILE4.R)
this.path:::.cat.file(FILE2.Rmd, number.nonblank = TRUE,
squeeze.blank = TRUE, show.tabs = TRUE,
show.command = TRUE)
this.path:::.cat.file(FILE3.md, number.nonblank = TRUE,
squeeze.blank = TRUE, show.tabs = TRUE,
show.command = TRUE)
unlink(c(FILE3.md, FILE2.Rmd))
}
## 'sys.path()' works with 'compiler::loadcmp()'
if (requireNamespace("compiler")) {
FILE2.Rc <- tempfile(fileext = ".Rc")
compiler::cmpfile(FILE1.R, FILE2.Rc)
oenv2 <- this.path:::.envvars(R_PROMPT = "FILE2.Rc> ",
R_CONTINUE = "FILE2.Rc+ ")
compiler::loadcmp(FILE2.Rc)
this.path:::.envvars(oenv2)
unlink(FILE2.Rc)
}
## 'sys.path()' works with 'box::use()'
if (requireNamespace("box")) {
FILE2.R <- tempfile(fileext = ".R")
this.path:::.write.code(bquote({
## we have to use box::set_script_path() because 'package:box'
## does not allow us to import a module by its path
script_path <- box::script_path()
on.exit(box::set_script_path(script_path))
box::set_script_path(.(normalizePath(FILE1.R, "/")))
box::use(module = ./.(as.symbol(this.path::removeext(
this.path::basename2(FILE1.R)
))))
box::unload(module)
}), FILE2.R)
source(FILE2.R, echo = TRUE, spaced = FALSE, verbose = FALSE,
prompt.echo = "FILE2.R> ", continue.echo = "FILE2.R+ ")
unlink(FILE2.R)
}
## 'sys.path()' works with 'Rscript'
## it also works with other GUIs but that is
## not possible to show in a simple example
this.path:::.Rscript(c("--default-packages=NULL", "--vanilla", FILE1.R))
this.path:::.envvars(oenv)
## 'sys.path()' also works when 'source()'-ing a URL
## (included tryCatch in case an internet connection is not available)
tryCatch({
source(paste0("https://raw.githubusercontent.com/ArcadeAntics/",
"this.path/main/tests/sys-path-with-urls.R"))
}, condition = this.path:::.cat.condition)
for (expr in c("sys.path()",
"sys.path(default = NULL)",
"sys.dir()",
"sys.dir(default = NULL)",
"sys.dir(default = getwd())"))
{
cat("\n\n")
this.path:::.Rscript(c("--default-packages=this.path",
"--vanilla", "-e", expr))
}
## an example from R package 'logr'
this.path::sys.path(verbose = FALSE, default = "script.log",
else. = function(path) {
## replace extension (probably .R) with .log
this.path::ext(path) <- ".log"
path
## or you could use paste0(this.path::removeext(path), ".log")
})
unlink(FILE1.R)
Run the code above in your browser using DataLab