Course sections

What is R?, Lecture 5

R Workspace

The workspace is the current R working environment and includes any user-defined objects (vectors, matrices, data frames, lists, functions). At the end of an R session, the user can save an image of the current workspace that is automatically reloaded the next time R is started. Commands are entered interactively at the R user prompt. Up and down arrow keys scroll through the command history.
R can store the image of the entire workspace. After installing the software, create a folder for the workspace and copy the folder path. Then do a right click on the R short cut and select properties. Paste the folder path in “start in”. Workspace and working directory

Workspace, .RData
As a beginning R user, it’s OK to consider your workspace “real”. Very soon, you will evolve to the next level, where you will consider your saved R scripts as “real”. With the input data and the R code used, one can reproduce everything. One can make the analysis fancier. One can get to the bottom of puzzling results and discover and fix bugs in the code and can reuse the code to conduct similar analyses in new projects.

Because it can be useful sometimes, note the commands you’ve recently run appear in the History pane.
But you don’t have to choose right now and the two strategies are not incompatible. Let’s demo the save / reload the workspace approach. Upon quitting R, you have to decide if you want to save your workspace, for potential restoration the next time you launch R. Depending on your set up, R or your IDE, eg RStudio, will probably prompt you to make this decision.

Quit R/Rstudio, either from the menu, using a keyboard shortcut, or by typing q() in the Console. You’ll get a prompt like this:
Save workspace image to ~/.Rdata?
Note where the workspace image is to be saved and then click Save.

Visit the directory where image was saved and verify there is a file named .RData. You will also see a file .Rhistory, holding the commands submitted in your recent session.
Restart RStudio. In the Console you will see a line like this:

[Workspace loaded from ~/.RData]

indicating that your workspace has been restored. Look in the Workspace pane and you’ll see the same objects as before. In the History tab of the same pane, you should also see your command history. You’re back in business. This way of starting and stopping analytical work will not serve you well for long but it’s a start.

Working directory
The working directory is the default location for all file inputs and outputs.
# returns path for the current working directory

# set the working directory to a specified directory

For example, if I call getwd() the file path “/Users/bradboehmke/Desktop/Personal/Data Wrangling” is returned. If I want to set the working directory to the “Workspace” folder within the “Data Wrangling” directory I would use setwd(“Workspace”). Now if I call getwd()again it returns “/Users/bradboehmke/Desktop/Personal/Data Wrangling/Workspace”.

Any process running on your computer has a notion of its “working directory”. In R, this is where R will look, by default, for files you ask it to load. It also where, by default, any files you write to disk will go. Chances are your current working directory is the directory we inspected above, i.e. the one where RStudio wanted to save the workspace.

You can explicitly check your working directory with:
It is also displayed at the top of the RStudio console.
As a beginning R user, it’s OK let your home directory or any other weird directory on your computer be R’s working directory. Very soon, I urge you to evolve to the next level, where you organize your analytical projects into directories and, when working on project A, set R’s working directory to the associated directory.

WhatsApp chat