1.1 Installation of R, RStudio and R Packages

1.1.1 Download and Install

As a first step, you need to download R and RStudio, whose links are as follows. For both software, you need to choose the version that corresponds to your operation system.

Download R: https://cloud.r-project.org/

Download RStudio: https://rstudio.com/products/rstudio/download/#download

RStudio is an Integrated Development Environment for R, which is powerful yet easy to use. Throughout this book, you will use RStudio instead of R to learn R programming. Next, let’s get started with a quick tour of RStudio.

1.1.2 RStudio Interface

After opening RStudio for the first time, you may find that the font and button size is a bit small. Let’s see how to customize the appearance.

a. Customize appearance

On the RStudio menu bar, you can click Tools, and then click on Global Options as shown in the following figure.

Global Options

Figure 1.1: Global Options

Then, you will see a window pops up like Figure 1.2. After clicking on Appearance, you can see several drop-down menus including Zoom and Editor font size, among other choices shown.

  • Zoom controls the overall scale for all elements in RStudio interface, including the sizes of menu, buttons, as well as the fonts.

  • Editor font size controls the size of the font only in the code editor.

After adjusting the appearance, you need to click on Apply to save our settings.

Zoom and Editor font size

Figure 1.2: Zoom and Editor font size

Here, we change the Zoom to 150% and set the Editor font size to 18.

b. Four panels of RStudio

Now, the RStudio interface is clearer with bigger font size. Although RStudio has four panels, not all of them are visible to us at the beginning (Figure 1.3).

Unfold panels

Figure 1.3: Unfold panels

In Figure 1.3, we have labeled three useful buttons as 1, 2, and 3. By clicking buttons 1 and 3, you can reveal the two hidden panels.1 By clicking button 2, we can clear the content in the bottom left panel as shown in the following figure.

Four panels

Figure 1.4: Four panels

Now, let’s take a close look at all four panels, which are labeled as 1-4 in Figure 1.4. You can change the size of each panel by dragging the two blue slides up or down and the green slide left or right.

  • Panels 1 and 2 are located to the left of the green line, and are collectively called the Code Area. We will introduce them in the following parts of this section.

  • Panels 3 and 4 are located to the right of the green line, and are collectively called R Support Area. We will introduce these two panels in later sections.

c. Console

Now, let’s introduce the panel 2 in Figure 1.4, which is usually called the Console.

By clicking the mouse on the line after the > symbol, you can see a blinking cursor, indicating that R is ready to accept codes. Let’s type 1 + 2 and press Return (on Mac) or Enter (on Windows).

It is a good habit to add spaces around an operator to increase readability of the code.

Writing code in the console

Figure 1.5: Writing code in the console

Hooray! You have successfully ran our first piece of R code and gotten the correct answer 3. Note that the blinking cursor now appears on the next line, ready to accept a new line of code.

R code(2)

Figure 1.6: R code(2)

The curious you may found that there is a [1] showing before the result 3. In fact, the [1] is an index indicator, showing the next element has an index of 1 in this particular object. We will revisit this point when we introduce vectors that contain more than one elements.

Although the console may work well for some quick calculations, you need to resort to the panel 1 in Figure 1.4 (usually called the Editor) to save our work and run multiple lines of code at the same time.

d. Save R codes as scripts

The Editor panel is the go-to place to write complicated R codes, which you can save as R scripts for repeated use in the future.

Firstly, we will introduce how to run codes in scripts. Let’s go to the editor and type 1 + 2. To run this line of code, you can click the Run button. The keyboard shortcut of running this line of code is Cmd+Return on Mac or Ctrl+Enter on Windows.

script

Figure 1.7: script

RStudio will then send the line of code to the console and execute the code. You can also run multiple lines of code by select the lines and click the Run button or use the keyboard shortcut.

After finishing writing codes in the editor, you can save them as a script. To do that, you can click the Save button as shown in the Figure 1.8. The keyboard shortcut of saving files is Cmd+S on Mac or Ctrl+S on Windows.

Save (I)

Figure 1.8: Save (I)

Then you would see a pop-up file dialog box, asking you for a file name and location to save it to. Let’s call it lesson1.1 here.

Save (II)

Figure 1.9: Save (II)

After saving files successfully, you can confirm the name of the R script on the top.

Save (III)

Figure 1.10: Save (III)

Lastly, if you want to create a new R script, you can click the + button on the menu, then select R Script. Then you will see a new file created. Add a screenshot here Note that there are quite a few other options including R Markdown, which will be introduced in Section 12.

create a new script

Figure 1.11: create a new script

1.1.3 Install and load R packages

Now, you have had a basic understanding of RStudio, it is time to introduce R packages, which greatly extend the capabilities of base R. There are a large number of publicly available R packages. As of July 2021, there are more than 17K R packages on Comprehensive R Archive Network (CRAN), with many others located in Bioconductor, GitHub, and other repositories.

To install an R package, you need to use a built-in R function , which is install.packages(). A function takes in arguments (inputs) and performs a specific task. After the function name, we always need to put a pair of parentheses with the arguments inside.

While there are many built-in R functions, R packages usually contain many useful functions as well, and we can also write our own functions, which will be introduce in Chapter ??.

With install.packages(), the argument is the package name with a pair of quotation marks around it. The task it performs is installing the specific package into R. Here, you will install the companion package for this book, named r02pro, a.k.a. R Zero to Pro. The r02pro package contains several data sets that will be used throughout the book, and interactive exercises for each subsection.

install.packages("r02pro")

If you miss the right parenthesis, R will show a plus on the next line (as shown in Figure 1.12), waiting for more input to complete the command. If this happens, you can either enter the right parenthesis, or press ESC to escape this command. When you see a blinking cursor after the > symbol, you can write new codes again.

Miss the right parenthesis

Figure 1.12: Miss the right parenthesis

After a package is installed, you still need to load it into R before using it. To load a package, you can use the library() function with the package name as its argument. Here, the quotation marks are not necessary.

library(r02pro)

Note that once a package is installed, you don’t need to install it again on the same machine. However, when starting a new R session, you would need to load the package again.

Quotation marks are necessary for installing R packages, but are not necessary for loading packages. If we install packages without quotation marks. We will see an error message, showing object not found.

install.packages(r02pro)

1.1.4 Exercises

  1. Which of the following code using to install packages into R will cause an error?
  • install.packages("r02pro")
  • install.packages(r02pro)
  1. Write R code to load the package r02pro

  2. Write R code to calculate 2 + 3.


  1. Note that you may see different panels hidden when you open RStudio for the first time, depending on the RStudio version. However, you can always reveal the hidden panels by clicking the corresponding buttons like Buttons 1 and 3 in Figure 1.3.↩︎