Working Directories and Workspaces in the RStudio IDE

Follow

Working Directories and Workspaces

The default behavior of R for the handling of .RData files and workspaces encourages and facilitates a model of breaking work contexts into distinct working directories. This article describes the various features of the RStudio IDE that support this workflow.

IMPORTANT NOTE: The RStudio IDE supports Projects, which make managing multiple working directories more straightforward. The features described below still work; however, Projects are now the recommended mechanism for dealing with multiple work contexts.

Default Working Directory

As with the standard R GUI, the RStudio IDE employs the notion of a global default working directory. Normally this is the user home directory (typically referenced using ~ in R). When RStudio starts up, it does the following:

  • Executes the .Rprofile (if any) from the default working directory.
  • Loads the .RData file (if any) from the default working directory into the workspace.
  • Performs the other actions described in R Startup.

When RStudio exits and there are changes to the workspace, a prompt asks whether these changes should be saved to the .RData file in the current working directory.

This default behavior can be customized in the following ways using the RStudio Options dialog:

  • Change the default working directory
  • Enable/disable the loading of .RData from the default working directory at startup
  • Specify whether .RData is always saved, never saved, or prompted for save at exit.

Changing the Working Directory

The current working directory is displayed by the RStudio IDE within the title region of the Console pane. You can also check your current working directory by running the command getwd() in the console.


There are a number of ways to change the current working directory:

  • Use the setwd R function
  • Use the Tools | Change Working Dir... menu (Session | Set Working Directory on a mac). This will also change directory location of the Files pane.
  • From within the Files pane, use the More | Set As Working Directory menu. (Navigation within the Files pane alone will not change the working directory.)

Be careful to consider the side effects of changing your working directory:

  • Relative file references in your code (for datasets, source files, etc) will become invalid when you change working directories.
  • The location where .RData is saved at exit will be changed to the new directory.

Because these side effects can cause confusion and errors, it's usually best to start within the working directory associated with your project and remain there for the duration of your session. The section below describes how to set RStudio's initial working directory.

Starting in Other Working Directories

If all of the files related to a project are contained within a single directory then you'll likely want to start RStudio within that directory. There are a number of ways (which vary by platform) to do this.

File Associations

On all platforms RStudio registers itself as a handler for .RData, .R, and other R related file types. This means that the system file browser's context-menu will show RStudio as an Open With choice for these files.

You can also optionally create a default association between RStudio and the .RData and/or .R file types.

When launched through a file association, RStudio automatically sets the working directory to the directory of the opened file. Note that RStudio can also open files via associations when it is already running—in this case RStudio simply opens the file and does not change the working directory.

Shortcuts (Windows)

On Windows, you can create a shortcut to RStudio and customize the "Start in" field. When launched through this shortcut RStudio will startup within the specified working directory.

Drag and Drop (Mac)

On Mac, dragging and dropping a folder from the Finder on the RStudio Dock icon will cause RStudio to startup with the dropped folder as the current working directory.

Run from Terminal (Mac and Linux)

On Mac and Linux systems you can run RStudio from a terminal and specify which working directory to startup within. Additionally, on Linux systems if you run RStudio from a terminal and specify no command line argument then RStudio will startup using the current working directory of the terminal.

For example, on the Mac you could use the following commands to open RStudio (respectively) in the '~/projects/foo' directory or the current working directory:

$ open -a RStudio ~/projects/foo
$ open -a RStudio .

On Linux you would use the following commands (note that no '.' is necessary in the second invocation):

$ rstudio ~/projects/foo
$ rstudio

Handling of .Rprofile

When starting RStudio in an alternate working directory the .Rprofile file located within that directory is sourced. If (and only if) there is not an .Rprofile file in the alternate directory then the global default profile (e.g. ~/.Rprofile) is sourced instead.

Loading and Saving Workspaces

If you want to save or load a workspace during an RStudio session you can use the following commands to save to or load from the .RData file in the current working directory:

> save.image()
> load(".RData")

Note that the load function appends (and overwrites) objects within the current workspace rather than replacing it entirely. Prior to loading you may therefore wish to clear all objects currently within the workspace. You can do so using the following command:

> rm(list=ls())

Note that since loading is handled at startup and saving is handled at exit, in many cases you won't require these commands. If however you change working directories within a session you may need them in order to sync your workspace with the directory you have chanaged to.

The RStudio Workspace menu also includes items that execute the above described commands, as well as enables you to load or save specific .RData files.

Handling of .Rhistory

The .Rhistory file determines which commands are available by pressing the up arrow key within the console. By default, RStudio handles the .Rhistory file differently than the standard R console or GUI, however RStudio can be configured to work the same was as those environments if you wish.

The conventional handling of .Rhistory files is as follows:

  • Load and save .Rhistory within the current working directory
  • Only save the .Rhistory file when the user chooses to save the .RData file

Whereas the default RStudio handling of .Rhistory files is:

  • Load and save a single global .Rhistory file (located in the default working directory)
  • Always save the .Rhistory file (even if the .RData file is not saved)

The RStudio defaults are intended to make sure that all commands entered in previous sessions are available when you start a new RStudio session. If you prefer the conventional R treatment of .Rhistory files you can customize this behavior using the General panel of the Options dialog.

Comments