9.4 The workspace

The workspace (aka your working environment) represents all of the objects and functions you have either defined in the current session, or have loaded from a previous session. When you started RStudio for the first time, the working environment was empty because you hadn’t created any new objects or functions. However, as you defined new objects and functions using the assignment operator <-, these new objects were stored in your working environment. When you closed RStudio after defining new objects, you likely got a message asking you “Save workspace image…?”" This is RStudio’s way of asking you if you want to save all the objects currently defined in your workspace as an image file on your computer.

9.4.1 ls()

If you want to see all the objects defined in your current workspace, use the ls() function.

# Print all the objects in my workspace
ls()

When I run ls() I received the following result:

## [1] "study1.df" "study2.df" "lm.study1" "lm.study2" "bf.study1"

The result above says that I have these 5 objects in my workspace. If I try to refer to an object not listed here, R will return an error. For example, if I try to print study3.df (which isn’t in my workspace), I will receive the following error:

# Try to print study3.df
#  Error because study3.df is NOT in my current workspace
study3.df
Error: object ‘study3.df’ not found

If you receive this error, it’s because the object you are referring to is not in your current workspace. 99% of the time, this happens because you mistyped the name of an object.