Skip to content
Prev 303275 / 398506 Next

set working directory to current source directory

On Mon, Aug 13, 2012 at 8:40 PM, Sachinthaka Abeywardana
<sachin.abeywardana at gmail.com> wrote:
You could use this:

source.and.set <- function(x) {
	setwd(dirname(x))
	source(x)
}
# use it like this:
source.and.set("/a/b/c.R")

or you could put this hack at the top of your source file in which
case whenever its source'd it will setwd to its directory:

this.dir <- dirname(parent.frame(2)$ofile)
setwd(this.dir)