The workspace refers to all the variables and functions (collectively called objects) that you create during an R session, as well as any packages that are loaded.

Often, you want to remind yourself of all the variables you’ve created in the workspace. To do this, use the ls() function to list the objects in the workspace. In the console, type the following:

> ls()
[1] "h"    "hw"    "x"    "y"    "yourname" "z"

R tells you the names of all the variables that you created.

One very nice feature of RStudio lets you examine the contents of the workspace at any time without typing any R commands. By default, the top-right window in RStudio has two tabs: Workspace and History. Click the Workspace tab to see the variables in your workspace, as well as their values.

How to manipulate the content of the R workspace

If you decide that you don’t need some variables anymore, you can remove them. Suppose that the object z is simply the sum of two other variables and no longer needed. To remove it permanently, use the rm() function and then use the ls() function to display the contents of the workspace, as follows:

> rm(z)
> ls()

Notice that the object z is no longer there.

How to save your work in the R workspace

You have several options for saving your work:

  • You can save individual variables with the save() function.

  • You can save the entire workspace with the save.image() function.

  • You can save your R script file, using the appropriate save menu command in your code editor.

Suppose you want to save the value of yourname. To do that, follow these steps:

  1. Find out which working directory R will use to save your file by typing the following:

    > getwd()
    [1] "c:/users/andrie"

    The default working directory should be your user folder. The exact name and path of this folder depend on your operating system.

    If you use the Windows operating system, the path is displayed with slashes instead of backslashes. In R, similar to many other programming languages, the backslash character has a special meaning. The backslash indicates an escape sequence, indicating that the character following the backslash means something special.

    For example, \t indicates a tab, rather than the letter t. Rest assured that, although the working directory is displayed differently from what you’re used to, R is smart enough to translate it when you save or load files. Conversely, when you type a file path, you have to use slashes, not backslashes.

  2. Type the following code in your console, using a filename like yourname.rda, and then press Enter.

    > save(yourname, file="yourname.rda")

    R silently saves the file in the working directory. If the operation is successful, you don’t get any confirmation message.

  3. To make sure that the operation was successful, use your file browser to navigate to the working directory, and see whether the new file is there.

How to retrieve your work in R

To retrieve saved data, you use the load() function. Say you want to retrieve the value of yourname that you saved previously.

First, remove the variable yourname, so you can see the effect of the load process:

> rm(yourname)

If you’re using RStudio, you may notice that yourname is no longer displayed in the Workspace.

Next, use load to retrieve your variable. Type load followed by the filename you used to save the value earlier:

> load("yourname.rda")

Notice that yourname reappears in the Workspace window of RStudio.