[R-pkg-devel] Question regarding finding the source file location without R-packages outside of R-Studio

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu Nov 23 21:07:14 CET 2023


On Thu, 23 Nov 2023 19:39:48 +0000
Tony Wilkes <tony_a_wilkes using outlook.com> wrote:

> This brings me to my actual question: is it possible to determine the
> source file location of an R script

This depends on how the script is being run. With source(file,
keep.source = TRUE), you can define a function and immediately ask for
the directory from its source reference:

self_dir <- getSrcDirectory(function() {})

With Rscript script.R / R -f script.R / R CMD BATCH script.R, source
references seem to be disabled due to the session not being
interactive, but you can reparse commandArgs(FALSE) and obtain the path
to the script.

It may be more reliable to explicitly launch an R process with the
environment variable R_LIBS set (and maybe also R_PROFILE and
R_ENVIRON) while project work is being done.

See also: https://CRAN.R-project.org/view=ReproducibleResearch for a
list of packages that help with project workflows.

(This may be a better question for R-help, since R packages don't run
as source files; after the package is installed, its compiled form
lives inside the lazy-load database.)

-- 
Best regards,
Ivan



More information about the R-package-devel mailing list