get_script_dir_path: Gets the path of the script that runs it

View source: R/get_script_dir_path.R

get_script_dir_pathR Documentation

Gets the path of the script that runs it

Description

Function returns the path to the script that ran it. Can be used to put the script outputs right next to the script. If run inside a function it will return the script that ran that function. Helps to run at the top of a sourced function: sample_script_path = get_script_path() And then use that script name in the function default: example_function = function(script_path = sample_script_path){script_path} Designed to work if run as Rscript, sourced via RStudio or console, or run via RStudio. Found most of the inspiration for this here: https://stackoverflow.com/questions/1815606/rscript-determine-path-of-the-executing-script

Usage

get_script_dir_path(
  debug_mode = FALSE,
  sourced_file = TRUE,
  include_file_name = FALSE
)

Arguments

debug_mode

Boolen on whether detailed messages should be output to help debug the code

sourced_file

Boolean on whether the files expects to be sourced or not. If so, and it's run as an rscript or sourced via the console it returns getwd.

include_file_name

Boolean on whther the file name should be reutrned if possible

Value

Returns the path to the script that ran it

Limitations

  • Only works through the console if it's sourcing a script that contains it (no script there)

  • Only works on the first file that runs it. After that all of the files sourced by that file would return the first path. The way around this is to source the secondary files with chdir = T. Then set sourced_file = T and it should work (the sourced files will use getwd() to get the path). For functions to know where they are. Write out a path for that function and call that path to source the function. The funciotn will then take that path as one of it's parameters. It will then have that path by default indefinitely.


Benjamin-Vincent-Lab/housekeeping documentation built on Feb. 17, 2024, 2:13 a.m.