Title: | Get the Location of the R Script that is Being Sourced/Executed |
Version: | 1.0.0 |
Author: | Naren Chandran Sakthivel
|
Maintainer: | Naren Chandran Sakthivel <narenschandran@gmail.com> |
Description: | Provides functions to retrieve the location of R scripts loaded through the source() function or run from the command line using the Rscript command. This functionality is analogous to the Bash shell's ${BASH_SOURCE[0]}. Users can first set the project root's path relative to the script path and then all subsequent paths relative to the root. This system ensures that all paths lead to the same location regardless of where any script is executed/loaded from without resorting to the use of setwd() at the top of the scripts. |
License: | MIT + file LICENSE |
Encoding: | UTF-8 |
RoxygenNote: | 7.2.3 |
Depends: | R (≥ 3.6.0) |
Suggests: | knitr, rmarkdown, prettydoc |
VignetteBuilder: | knitr |
NeedsCompilation: | no |
Packaged: | 2023-08-17 08:11:58 UTC; naren |
Repository: | CRAN |
Date/Publication: | 2023-08-17 15:00:02 UTC |
Return directory where the script exists
Description
This is a convenient wrapper to dirname(scriptloc())
Usage
script_dir_get()
Value
Returns either a single string with path to the file being executed, or NULL
Examples
writeLines("library(scriptloc); script_dir <- script_dir_get(); print(script_dir)", "dir-example.R")
source("dir-example.R")
file.remove("dir-example.R")
Get location of script that was executed through Rscript
Description
When a script is being called from the command line using Rscript, the relative path of the script being called will be present in the command line arguments passed to the file. This is used to get the relative path of the script.
Usage
script_file_get(cargs)
Arguments
cargs |
- Output from commandArgs(trailingOnly = F) |
Get location of script in a fashion analagous to $BASH_SOURCE[0]
Description
There are two ways in which code from an R file can be executed: through the command line by invoking Rscript or by using the source() function. This function tries to see if either of the methods were used, and if not, deduces that the function is being called from an interactive session and therefore returns NULL.
Usage
scriptloc()
Value
Returns either a single string with path to the file being executed, or NULL
Examples
writeLines("library(scriptloc); script_path <- scriptloc(); print(script_path)", "example.R")
source("example.R")
file.remove("example.R")
Get location of script that was loaded through the source() function
Description
Whenever a script is sourced into an R session using source(), the path of the file gets attached to the environment with the name ofile, which can be used to get the file path from within the script. If multiple files are being sourced before, then these would all be present in a different environment. Getting the ofile entry that comes last will give us the sourced file.
Usage
src_file_get(frames)
Arguments
frames |
: This is the output from sys.frames() |
Note
If any file defines a variable called ofile, this can potentially interfere with detection of the filepath in the main user-facing function.
# Specific bad example
# a.R # —- ofile <- "something" script_path <- scriptloc()
# b.R # — source("a.R") print(script_path) # Would print "something" and not "a.R"