R’s working directory

[This article was first published on Brokering the Closure » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 filesRR 2.7.0bin.

    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?


Posted in R

To leave a comment for the author, please follow the link and comment on their blog: Brokering the Closure » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)