Skip to content
Back to formatted view

Raw Message

Message-ID: <CAP01uRmv_ifHHeo6QY0xYUoesTETVvcmECc0A2jGtw6PTeqXOg@mail.gmail.com>
Date: 2012-08-14T01:01:32Z
From: Gabor Grothendieck
Subject: set working directory to current source directory
In-Reply-To: <CAGuusR_AE6Z6PVod95Ypf8wbmj+V5rTnFKqPQW+=SZkA0PjaXg@mail.gmail.com>

On Mon, Aug 13, 2012 at 8:40 PM, Sachinthaka Abeywardana
<sachin.abeywardana at gmail.com> wrote:
> Hi all,
>
> Is there a way to get cran R to set the working directory to be wherever
> the source file is? Each time I work on a project on different computers I
> keep having to set the working directory which is getting quite annoying.
>
> Thanks,
> Sachin
>

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)


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com