[R] setting new working directories

Jim Lemon jim at bitwrit.com.au
Fri Jan 5 09:02:20 CET 2007


Bill Shipley wrote:
> Hello, and Happy New Year.  My default working directory is getting very
> cluttered.  I know that I should be using a different working directory for
> each project (I work in Windows), but do not know how to go about creating
> different ones and moving back and forth between them.  I have read Venables
> & Ripley (Modern Applied Statistics with S-PLUS, 1994) but this seems out of
> date with respect to this topic and have searched through the documentation
> but cannot find a clear explanation for doing this.  Can someone point me to
> the proper documentation for creating and using different working
> directories from within Windows (please, no comments about switching to
> UNIX...).
> Thanks.
> 
I'm not sure to which New Year you refer, but thanks and the same to you.

I think you may want to automatically start up R in a directory for each 
project. There is a discussion of that in Kickstarting R at:

http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_start.html

Basically, you can create icons on the desktop that will start R in a 
number of directories. In Windows, don't worry about adding the line to 
your "R" startup file, just set the "Start Program in" to the desired 
directory in the Properties dialog of the shortcut.

You can also set up an R file that gives you an interactive choice when 
the program starts. Create a file like this:

cat("R programming\n")
cat("Animals\n")
cat("Vegetables\n")
cat("Minerals\n")
cat("Type in the first letter of the project -")
answer<-toupper(strsplit(readline(""))[1])
if(answer == "R") setwd("c:/jim/R/programs")
if(answer == "A") setwd("c:/jim/things/animals")
...

and say you name it c:\jim\R\SelectProject.R
(notice that Windows uses backslashes but you use slashes in R)

put the following line in your .First function:

source("c:/jim/R/SelectProject.R")

and you should be able to select your project and directory on startup.

Jim



More information about the R-help mailing list