2.3 Starting a session in RStudio
2.3.1 Setting the global options
It is possible to customize RStudio (e.g., code, appearance, organization of panels) to adapt it to our needs and preferences. To do so, click on the main RStudio menu bar \(\rightarrow\) Tools \(\rightarrow\) Global Options. Although there are many available options to customize RStudio sessions, we will mention below some of the most interesting features to inspect in the global options settings:
General: It is convenient to start a new R session to avoid carrying over variable corruptions. Consequently, do not restore or save .RData files into the workspace at startup.
Code-Display options: To enhance the usability experience, enable soft-wrap R source files, highlight selected word, show line numbers, and blinking cursor. In the option show margin, set the margin column to 60.
Appearance: The customization of the editor and console's appearance (e.g., font size) is pivotal as we will spend a lot of time looking at the computer screen when programming. For example, the use of dark background colors (e.g., material, chrome) is very popular among programmers because it minimizes eye strain or fatigue. If you are a low vision user, the high contrast generated by a dark background and white text will be beneficial as well.
Pane Layout: It is possible to reorganize the location of the panels. Likewise, it is possible to show or hide the different tabs available in the global environment and notebook. For instance, we could arrange the source on the upper left-hand side, inspecting the output of the code that we run underneath or on the right hand side (the console would be located on the bottom left corner or on the upper right corner respectively).
2.3.2 First lines of code
Every session will start with three lines of code that we will include at the beginning of our R script. First, we will use the function rm()
(i.e., remove) to delete any R object stored in the global environment. We want to start each session anew, without carrying over R objects from previous sessions. Second, we will set the randomization seed with the function set.seed()
to reproduce the R objects and outputs of any simulated data and computation. Last, we will turn off the scientific notation used in R.
rm(list = ls())
set.seed(1234)
options(scipen=999)
2.3.3 Setting the working directory
There is a point-and-click approach to set the working directory for each R session. To do so, click on the main RStudio menu bar \(\rightarrow\) Session \(\rightarrow\) Set Working Directory \(\rightarrow\) Choose Directory. Then, select the desired folder to store all the files of the session (e.g., the R file with the code, data sets).
When exporting plots and files, these files will be saved and stored locally in the working directory already set at the beginning of the session. For example, we created a folder called R located in the main SSD MacOS/C: hard drive that includes the directories of all our R projects. At a lower level, we created another folder called psyc3012 (Data Science with R) to store the current R project that, in turn, includes one folder per session (e.g., session1, session2, session3).
Setting the path for the working directory
- The path should be short
- Create a folder called R in your PC (C:) or MacOS (HD/SSD) hard drives. Then, create folders for different projects in the R folder
- Use short names with no spaces (e.g., do not use a folder name such as session 1, but session1 or session_1)
- R is case sensitive (psyc3012 differs from PSYC3012)
- Use single ('x') or double quotes ("x") because the path is a character string
The function getwd()
finds the path to the folder of our current working directory, whereas the function setwd()
sets the working directory's path.
getwd()
setwd('~/R/psyc3012/session1')