Title: | Get the Location of the R Script that is Being Sourced/Executed |
---|---|
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. |
Authors: | Naren Chandran Sakthivel [aut, cre] |
Maintainer: | Naren Chandran Sakthivel <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.0 |
Built: | 2024-11-10 03:44:06 UTC |
Source: | https://github.com/cran/scriptloc |
This is a convenient wrapper to dirname(scriptloc())
script_dir_get()
script_dir_get()
Returns either a single string with path to the file being executed, or NULL
writeLines("library(scriptloc); script_dir <- script_dir_get(); print(script_dir)", "dir-example.R") source("dir-example.R") file.remove("dir-example.R")
writeLines("library(scriptloc); script_dir <- script_dir_get(); print(script_dir)", "dir-example.R") source("dir-example.R") file.remove("dir-example.R")
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.
script_file_get(cargs)
script_file_get(cargs)
cargs |
- Output from commandArgs(trailingOnly = F) |
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.
scriptloc()
scriptloc()
Returns either a single string with path to the file being executed, or NULL
writeLines("library(scriptloc); script_path <- scriptloc(); print(script_path)", "example.R") source("example.R") file.remove("example.R")
writeLines("library(scriptloc); script_path <- scriptloc(); print(script_path)", "example.R") source("example.R") file.remove("example.R")
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.
src_file_get(frames)
src_file_get(frames)
frames |
: This is the output from sys.frames() |
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"