Determine path of the executing script

Frank picture Frank · Nov 29, 2009 · Viewed 124.7k times · Source

I have a script called foo.R that includes another script other.R, which is in the same directory:

#!/usr/bin/env Rscript
message("Hello")
source("other.R")

But I want R to find that other.R no matter what the current working directory.

In other words, foo.R needs to know its own path. How can I do that?

Answer

this.is.not.a.nick picture this.is.not.a.nick · Apr 16, 2013

Here there is a simple solution for the problem. This command:

script.dir <- dirname(sys.frame(1)$ofile)

returns the path of the current script file. It works after the script was saved.