How to get R to recognize your working directory as its working directory?

Dan Goldstein picture Dan Goldstein · Sep 8, 2009 · Viewed 83k times · Source

I use R under Windows on several machines.

I know you can set the working directory from within an R script, like this

setwd("C:/Documents and Settings/username/My Documents/x/y/z")

... but then this breaks the portability of the script. It's also annoying to have to reverse all the slashes (since Windows gives you backslashes)

Is there a way to start R in a particular working directory so that you don't need to do this at the script level?

Answer

Marek picture Marek · Sep 8, 2009

You should copy shortcut to R (R.lnk file) to desire folder. Then in "Properties" (right mouse button -> last option) delete anything in field "Start in..." in second tab ("Shortcut"?). If you start R with this shortcut working directory will be that one where the shortcut is.

I don't have english version of Windows so I'm not sure about field names, but they should be easy to find.

Similar questions were in R-windows-faq:

2.5 How do I run it?

2.10 How can I keep workspaces for different projects in different directories?

2.14 What are HOME and working directories?

In 2.14 is mentioned that

The working directory is the directory from which Rgui or Rterm was launched, unless a shortcut was used when it is given by the `Start in' field of the shortcut's properties.