An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120814/a3737be5/attachment.pl>
set working directory to current source directory
5 messages · Sachinthaka Abeywardana, Kenn Konstabel, Gabor Grothendieck +2 more
When you close R it asks whether to save the workspace. I often say
"yes" and later start R by double clicking on that workspace (named
.Rdata) -- then the wd is automatically set. Alternatively you can
have setwd("X:/some/thing") at the beginning of your source file.
On Tue, Aug 14, 2012 at 3:40 AM, 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
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
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
Hi Sachin,
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.
a while ago I asked a somewhat similar question on stackoverflow: http://stackoverflow.com/questions/8835426/get-filename-and-path-of-sourced-file You may want to have a look at the suggestions I got. Best, Claudia
Claudia Beleites Spectroscopy/Imaging Institute of Photonic Technology Albert-Einstein-Str. 9 07745 Jena Germany email: claudia.beleites at ipht-jena.de phone: +49 3641 206-133 fax: +49 2641 206-399
3 days later
Subject: Re: [R] set working directory to current source directory
A windows-centric work-round (assuming .Rdata files are 'associated' with the right R binary n installation) is to save an empty environment in the relevant project directory with the source file you would normally run. Starting R by clicking on .Rdata file opens R in the right directory to start with. Using relative paths in scripts thereafter then keeps things within the project work area. Starting the first script with rm(list=ls()) guards against inadvertent inclusion of old data (and even apparently 'empty' environments aren't necessarily empty, so I clean up regardless if I start a session that way).
S Ellison
*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}