2  RStudio Projects

When we have finished this Chapter, we should be able to:

Learning objectives
  • Work with R projects.
  • Execute R code directly from the Source Editor.
  • Document the code using comments.
  • Save our code in an R script.

2.1 Working with RStudio Projects

One of the advantages of RStudio IDE is that allows us to work with RStudio Projects. The RStudio Projects are recommended for the following main reasons:

  • When we are working in R, the program needs to know where to find inputs (e.g. datasets) and deliver outputs (e.g. results, figures), and it will search first in what is called a “working directory”. When the RStudio session is running through the project file (.Rproj), the current working directory points to the project’s root folder.

  • RStudio Project is a powerful feature that enables to organize all the files and switch between different projects and tasks without getting the datasets, code scripts, or output files all mixed up.

2.1.1 Creating an RStudio Project

Let’s create our first RStudio Project to use for the rest of this textbook. From the RStudio menu select (Figure 2.1):

flowchart LR
  A(File) -.-> B(New Project...)

Figure 2.1: Create an RStudio Project using the RStudio’s menu

Alternatively, we can use the plus project icon or we can select New Project... from the top right Project menu (Figure 2.2):

Figure 2.2: Create an RStudio Project using the RStudio’s Project menu

Then, we follow the steps in Figure 2.3:

(a) Step 1
(b) Step 2
(c) Step 3
Figure 2.3: Steps to create an RStudio Project.

In Step 3 (Figure 2.3 c) the directory name that we type will be the project’s name. We call it whatever we want, for example “my_first_project”.

Once we have completed this process, R session switches to the new RStudio Project with the name “my_first_project” (Figure 2.4):

Figure 2.4: The new RStudio Project has been created with the name “my_first_project”.

2.1.2 Organizing our RStudio Project folder

Files on our computer are organized into folders. Similarly, RStudio project folders and files can be managed in the same manner as we typically handle folders and files on our computer. For our purpose, it is sufficient to consider a simple RStudio Project folder that contains the following sub-folders (Figure 2.5):

  • data: we save data files of any kind, such as .csv, .xlsx, .txt, etc.
  • figures: we save plots, diagrams, and other graphs
Figure 2.5: Schematically presentation of the folder structure of a minimal RStudio project.
INFO

The file named my_first_project.Rproj, which has been created by RStudio automatically in our project folder, contains information of the project and can also be used as a shortcut for opening the project directly from the file system in our computer.

We can create new folders (sub-folders) in the main RStudio Project folder using the (Figure 2.6).

(a) Create a sub-folder with the name ‘data’
(b) Create a sub-folder with the name ‘figures’
Figure 2.6: We can use the “New Folder” icon to add sub-folders into RStudio Project.

Therefore, we end up to the following RStudio Project folder structure:

Figure 2.7: A minimal RStudio Project folder structure.

2.2 Opening a new R script

Typically, R code is written in R script files, denoted by the .R extension. An R script is simply a text file in which the R code is saved, and then it can be executed on the R console. The benefits of using R script files include the ability to execute code chunks rather than individual lines facilitating code reuse and organization. Furthermore, R script files enable easy documentation through one-line comments prefixed with the hashtag symbol, #, and facilitate code sharing with others.

To open an R script in RStudio, we navigate to the menu bar at the top of the RStudio window and follow these steps:

flowchart LR
  A(File) -.-> B(New File) -.-> C(R Script)

Figure 2.8: Open a new R script from the Rstudio menu.

Alternatively, we can use the plus icon from RStudio toolbar or the keyboard shortcut Ctrl+Shift+N for Windows/Linux or Cmd+Shift+N for Mac.

Another pane, the “Source Editor”, is opened on the left above the Console pane (Figure 2.9). In Source Editor, we can write a length script with lots of code chunks and save the file for future use (at present, the new unsaved R script is named “Untitled 1”).

Figure 2.9: RStudio Screenshot with four panes.

We can change the size of the panes by either clicking the minimize or maximize buttons on the top right of each pane, or by clicking and dragging the middle of the borders of the panes.

The four panes might be placed in a different order that those in Figure 2.9. If we would like, we can change where each pane appears within RStudio under the RStudio preferences. We select from RStudio menu (Figure 2.10):

flowchart LR
  A(Tools) -.-> B(Global Options) -.-> C(Pane layout)

(a) Step 1: We select Tools -> Global Options.
(b) Step 2: We can change the order of panes and check which tabs we would like to appear within each pane.
Figure 2.10: Options for the apperance of RStudio panes.

Now, let’s type 14 + 16 at a new R script in the Source Editor pane and press the button1. The result is printed in the Console (Figure 2.11):

1 In .R script, we can execute our code line by line (by putting the cursor on the line) or selecting a chunk of lines (by highlighting the code) and pressing the run button in the Source Editor. We can also run our selected code using the keywboard shortcut Ctrl+Enter for Windows/Linux or Cmd+Enter for Mac.

Figure 2.11: We can write our code in the source editor and get the output in the console.

2.3 Adding comments to a script

Comments can also be used to explain R code, and to make the script more readable. They can also be used to prevent execution when testing alternative code (Figure 2.12).

Figure 2.12: The script pane with comments.

Comments start with the hashtag symbol #. When executing the R-code, R will ignore anything that starts with #. It is considered good practice to comment our code when working in an .R script.

Keyboard Shortcut for commenting in/out multiple lines at a time:

  • Ctrl+Shift+C for Windows/Linux

  • Cmd+Shift+C for Mac

Finally, we can save our R script in the RStudio Project folder. The simplest way is to click on the save icon , give a file name to the script such as “my_script” and then press the “save” button to store it in “my_first_project” folder (Figure 2.13).

Figure 2.13: Saving our R script in the RStudio Project folder.

Now, the folder structure of our RStudio Project should include the following items (Figure 2.14):

NOTE: The .Rhistory file contains a history of code that has been executed and has been created automatically by RStudio.
Figure 2.14: Folder structure of our RStudio Project with sub-folders and R script.

Note that if we close the R script, we can re-open it by clicking on the “my_script” file from the “Files” tab.