Working Directories and Workspaces - RStudio Support PDF
Working Directories and Workspaces - RStudio Support PDF
RStudio Support > How To Articles > Using the RStudio IDE 🔍 Search
Using Projects
Working Directories and Workspaces Importing Data with RStudio
Customizing RStudio
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 Using the Data Viewer
article describes the various features of RStudio which support this workflow. Version Control with Git and SVN
IMPORTANT NOTE: In version v0.95 of RStudio a new Projects feature was introduced
to 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.
To check your current working directory, you can run the command getwd() in the
RStudio console.
As with the standard R GUI, RStudio 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:
https://support.rstudio.com/hc/en-us/articles/200711843-Working-Directories-and-Workspaces 1/4
8/7/2020 Working Directories and Workspaces – RStudio Support
The current working directory is displayed by RStudio within the title region of the
Console. There are a number of ways to change the current 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.
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.
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.
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.
https://support.rstudio.com/hc/en-us/articles/200711843-Working-Directories-and-Workspaces 2/4
8/7/2020 Working Directories and Workspaces – RStudio Support
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:
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.
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.
https://support.rstudio.com/hc/en-us/articles/200711843-Working-Directories-and-Workspaces 3/4
8/7/2020 Working Directories and Workspaces – RStudio Support
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
https://support.rstudio.com/hc/en-us/articles/200711843-Working-Directories-and-Workspaces 4/4