Skip to content

R’s working directory

October 3, 2008

Do you usually start R with a desktop icon or some other shortcut? Are you tired of using setwd and getwd each time after you start R to get the working directory correctly? If so, then your days of suffering might be just coming to an end.

Having the working directory set correctly is very convenient. You can both read and write files to the proper place without typing (on Windows, usually very long) path names. There are couple of solutions:

  1. Use setwd in scripts.

    One way to achieve this is to have a setwd function call at the top of your scripts. You then run it every time you do the computations in that script. For example to have at the top of a file a following line:

    setwd("c:/path/to/my/directory/")

    It is a nice approach, but things get complicated if you move files to different computers, say from home to your office, and have different directory structures, disk names etc. Of course you can change it every time. Or perhaps keep couple of versions and have all of them but one commented, for example:

    # setwd("c:/path/to/my/directory/at/home")
    setwd("c:/path/to/my/directory/at/work")

    Which is also OK, but for me is too much micromanagement. Also, it becomes a problem if the script is not intended for interactive use.

  2. Use Windows shortcuts.

    An alternative might be work with Windows shortcuts for starting R. In shortcut’s properties there is a “Start in” field in which you can put the path to the desired folder. If you start R with the modified icon then R’s working directory will be correctly set. With that approach you can have, say, couple of R icons on your desktop, each to different project folders.

    This is convenient unless you work on 10 projects. Each time you may have to create yet another shortcut.

  3. Use PATH environment variable.

    Another approach is to set the environment PATH variable. If you add the path to R’s executable to it then you will be able to start R from whatever directory in the system you want.

    To modify the PATH variable you need to right-click on “My Computer” and select “Properties” then go to “Advanced” tab and “Environment variables” button. The way to modify the PATH variable depends on where you installed R. Usually it is something like c:\program files\R\R 2.7.0\bin.

    I use this approach myself with Total Commander and its command line. Wherever I am on the disk I can start R in that directory just bu typing rgui and pressing Enter. You can also use Windows Console (cmd) for that.

  4. Make a context menu option.

    Yet another way is to add a command to your context menu (the one appearing when you right-click on things). By right-clicking on a folder and choosing “R” option you can start R with that folder set as the working directory.

    To set up such a command you have to modify Windows Registry and will require, I believe, administrative privileges. Look here for details how to do this.

Any other ideas or suggestions?

About these ads

Comments are closed.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: