Skip to content

Instantly share code, notes, and snippets.

@sjessa
Created July 13, 2016 17:48
Show Gist options
  • Save sjessa/96b48caaef2e1df425d9124a0c464e2b to your computer and use it in GitHub Desktop.
Save sjessa/96b48caaef2e1df425d9124a0c464e2b to your computer and use it in GitHub Desktop.
dynamically source a file in R
# Source a file in the same directory as the script regardless of the
# working directory from which the script is being called.
# Works in conjunction with argparser.
# Adapted from http://stackoverflow.com/a/1815743
command <- commandArgs(trailingOnly = FALSE)
script_name <- sub("--file=", "", command[grep("--file=", command)])
script_path <- dirname(script_name)
my_functions <- file.path(script_path, "functions.R")
source(my_functions, chdir = TRUE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment