Link Search Menu Expand Document (external link)

RStudio Session Management

Table of contents


Key Concepts

To work effectively in RStudio, you need to keep in mind the following four components:

Working Directory

Whenever you are working on a project, your code (scripts and markdown files), workspace (data structures), commandline history, and data files are located somewhere on your filesystem.

You need to know where you are in order to keep your work organized!

It is recommended that you keep each of your projects in a separate folder containing all of the scripts, data, and any other relevant files pertaining to that project.

For example, it’s a good idea to create a parent folder called something like “R_homework”, and then create a separate folder for each week’s assignment. Fortunately, RStudio can help you organize your work using RProjects. More on this below.

Workspace

All of the R data objects you create in your current session will be stored in a file with the extension .Rdata that can be reloaded next time you want to work on the same project. This will include anything like variables, vectors, data frames, etc.

Command History

R keeps a record of all the commands you have executed in the current R session. Your command history can be accessed from the Console by using the up and down keys, or you can select to re-run previous commands from the History window in the GUI.

Your commandline history is also saved as an .Rhistory file in your working directory. You may want to save your successful commands in a separate file (or script) because your history will also contain your mistakes!

R Projects

R Projects are an RStudio convenience that enables you to easily manage working directories, workspaces, and associated files for any R project.

Projects provide a super easy way to collect all of your files and data associated with a particular project in one place. You can use the Projects menu in the top right-hand corner of RStudio to create, save, and select R Projects.

Whenever a Project is opened, the working directory is automatically set to the location of the project folder, the workspace is loaded, and any scripts or data files are available. You should see all of these in the Files menu, along with the .Rproj file. If you click on the .Rproj icon in the Files menu, a new window will pop up that displays all the configuration options for the project.

It is highly recommended to use R Projects for all of your work.

Back to top


More on Workspaces

The workspace contains the different R objects and functions from your current R session only (not the code; the commandline history and any code in your R scripts or R markdown files are saved separately).

Workspaces can be saved to preserve this data and a saved workspace file can be loaded to start using the previously created objects in them again.

At the end of each session, or when you change RProjects, you will be asked whether you want to save your current workspace. If you’re just playing around, you won’t need to save the temporary data objects you’ve created. Otherwise it’s generally useful to save the state of the data structures you have created for a particular workflow.

Default Workspace

The default workspace is saved as .Rdata. This file will be automatically created when you fire up RStudio and you have not loaded a named workspace.

Named Workspace

A named workspace will have a filename of the form myproject.Rdata.

Instead of saving your current workspace with the default name .Rdata, you may save a workspace for a particular project you are working on as a named .RData file that you can load later.

If you are working in an RProject, a named .Rdata file will be created automatically when you set up the RProject and will be loaded each time you open the project.

Managing Workspaces

You may save and load workspaces manually, however if you are using an RProject (see below) all of this will be handled magically for you.

Key Commands

The key commands for managing workspaces are:

getwd() - returns an absolute filepath representing the current working directory of the R process

setwd(“path_to_dir”) - set the working directory to dir

dir.create(“path_to_dir”) - create a new subdirectory in your current working directory

load(“myproject.Rdata”) - load an existing (default or named) workspace

save.image() - save your current workspace

ls() - list the R data objects in your current workspace

q() - quit R (you will be prompted as to whether you want to save your current workspace)

To manually load a previously saved .RData file, set your current working directory to the one where .RData is located, and then use the load() command or use the RStudio menus to load it. You can do this using the Session menu or by clicking on the filename in the Files pane.

Examples

  • Code blocks below that include the R commandline prompt (>) show what was entered in the console window.
  • Comments begin with a hashtag (#).
  • The resulting output is shown below each command.
  • Note that some commands do not generate any output on the commandline.
> # Get working directory
> getwd()
[1] "/Users/kcg1/code/kriscgun/xdasi-bio-2021/exercises"

> # assign current dir to a variable
> CWD <- getwd()
# Create a folder to use for working directory
> dir.create("ex1")
Warning message:
In dir.create("ex1") : 'ex1' already exists
> # Change working directory
> setwd("ex1")
> # List the contents of your working directory
> dir()
[1] "example1.R"
> # Return to original directory
> setwd(CWD)
> # Save a (named) workspace
> save.image("Name_of_workspace.RData")
> # Load a workspace
> load("Name_of_workspace.RData")
>
> # Do not do this!
> load.image(".RData")
Error in load.image(".RData") : could not find function "load.image"

Workspace Hygiene

It is good practice to save workspaces for different projects, along with associated scripts and data files, in different directories (folders). You can navigate and manage directories using either the commandline or RStudio drop-down menus.

If you are working in an Rproject, the workspace associated with that project will be loaded automatically each time you open the project.


Back to top