5 Session Management

In this chapter I review some important aspects about managing your interactive session with R using RStudio.

Here’s what you should always keep in mind. From the point of view of a session, all the work, activities, and actions you do with R can be classified into three categories:

  • when starting a session

  • during the session

  • when closing a session

Because there are several things going on behind the scenes in each of the categories listed above, it is important that we talk about them—at least briefly.

5.1 Starting a Session

Starting a session can be done in two primary ways:

  • launching the R application program, which will give you access to its graphical usier interface; or via RStudio or any other IDE that has the ability to open an R session.

  • by clicking on a file that your computer associates with R (or RStudio). For example, R-script files (with file extension .R or .r), R-Markdown files (.Rmd extension), R-Noweb files (.Rnw extension), RStudio project files (.Rproj extension), etc.

5.1.1 What happens when you open an R session via RStudio?

This is an important question that many users never stop to think about. However, it’s worth reviewing what happens when a session is started. So let’s talk about this.

  • Every time you open RStudio, the console pane will display R’s welcome message.

  • The console is always linked to a working directory.

  • Typically, the working directory of the console will be your home directory, unless you specified a different location when you installed R in your computer.

  • You can change the working directory to a different directory if you want. This change can be permanent (for future sessions), or temporary (for a current session).

  • To permanently modify the working directory (when a session is opened), go to the menu bar, select “RStudio” tab, click on “Preferences”, and modify the “R General” options.

  • To temporarily change the working directory, go to the menu bar, select the “Session” tab, and click on “Set Working Directory”, or simply specify a working directory with the setwd() function executed from R’s console.

5.1.2 Opening a session for the first time

If you are opening a session in RStudio for the very fist time:

  • the “Editor” pane will be collapsed, and

  • all the tabs in the “Environment, History, Connections” pane will be empty

In general, when you open a session (not for the very fist time):

  • the “Environment” tab may display some objects, which means you have some existing objects in your global environment. You can also invoke the list function ls() to list any available objects in your current session:
# what objects are in my global environment?
ls()
  • the “History” tab may contain lines of previously used commands; if this is the case it means that there is an associated text file called .Rhistory in your working directory. You can also use the history() function to display the Commands History, that is, all the commands invoked in your interactive sessions:
# is my history of commands being tracked?
history()

5.1.3 Working Directory

If you open R via an application program (e.g. launching RStudio) that lets you interact with R’s console, your session will have an associated working directory, sometimes also referred to as the current working directory.

When you install R in your computer, during the installation process a default working directory is assigned to R. By default, this directory is your home directory. You can check whether this is the case if you run the get working directory function getwd() in your console.

# run this command in the console to find out 
# the working directory of your session
getwd()

In RStudio, you can also look at the console pane, and inspect the text right below the Console tab that always displays the working directory of your session. If you see the symbols ~/ it means that the home directory (represented by the tilde) is your working directory.

The working directory is indicated right below the Console tab.

Figure 5.1: The working directory is indicated right below the Console tab.

5.2 Working During a Session

Working with R involves a series of common actions:

  • writing commands (in a source document or in the console)

  • executing commands (from a source doc or from the console)

  • looking or examining outputs

  • reading or importing files (e.g. data files, script files)

  • writing or exporting/saving output to files (e.g. data files, images, results)

From the logistical point of vew, it all boils down to executing commands, taking into account the following:

  • where a command is being executed from

  • if the command requires an input, where does that input come from?

  • if the command produces an output, where does that output go to?

This is why we need to describe the following:

  1. Working Directory

  2. Workspace and Global Environment

  3. History of commands

5.2.1 Session’s Working Directory

You will be writing commands either directly in the console or in a source document (e.g. R script file, Rmd file, etc.)

The console is always associated to a working directory (usually your home directory).

A source document, once it has been saved, will live in some directory. Ideally, a source document’s directory would be used as its working directory, using relative file-paths to handle all input and output resources required in the code of the source document. Unfortunately, this ideal is far from what happens in practice.

  • By default, when you execute code chunks in a saved Rmd file, the working directory is the directory where the Rmd file resides in.

  • By default, when you execute code from an R file, the working directory is that of the console.

5.2.2 Workspace and Global Environment

Regardless of where commands are being executed from, R will carry out all the necessary computations, and objects will be created along the way.

The collection of objects that are being created (and kept alive) during a session are part of what is considered to be your workspace.

At a more technical level, all the objects in your workspace are part of an R environment. To be more precise, the workspace is the Global Environment.

On the console, if you type ls(), R will display all the available objects in your workspace.

# available objects in your workspace
ls()

You can also go to the pane “Environment, History, …” and click on the Environment tab to see the objects in your workspace which are displayed by default under the option “Global Environment”.

5.2.3 Commands History

When you start a session, R will track all the commands that you execute during that session. As you execute commands, they will become part of what is called the Commands History.

You can find the list of all used commands in the History tab, located in the Environment, History, Connections pane of RStudio. You can also access the commands history with the function history().

The comands history is avaialble in the History tab.

Figure 5.2: The comands history is avaialble in the History tab.

By default, the history of commands are stored in a text file called .Rhistory that is saved in your session’s working directory.

5.3 Closing a Session

At some point, the work that you’ve done in a session will come to an end, and consequently you will close the session.

When closing a session, what should you do?

This is a somewhat “very personal” type of question, because it is up to you to decide what should happen to all the work that you’ve done in R.

Having said that, you can always decide whether or not to:

  • save changes in your source document(s)

  • save the commands history in a text file

  • save the objects in your workspace (i.e. objects in Global Environment) in a binary file (native to R)

It turns out that RStudio comes with default actions that take place when you close a session:

  • it will ask you if you want to save changes in your source documents

  • it will ask you if you want to save the workspace in an .RData file (this is a file that uses R’s native binary format; this is saved in your session’s working directory)

  • it will automatically save your commands history in a text file called .Rhistory (saved in your session’s working directory)

Also, because of RStudio’s default settings, the next time you open a new session it will:

  • restore the previously open source document

  • restore objects in the .RData file into your workspace

  • give you access to all the commands stored in the .Rhistory file

Of course, you can change and customize the default settings of RStudio so that R/RStudio do certain things when closing a session.