1  R via RStudio

In this chapter, we introduce the RStudio Integrated Development Environment (IDE), a powerful tool widely used by R users. RStudio provides a user-friendly interface that makes it easier for both beginners and experienced users to write, debug, and execute R code.

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

Learning objectives
  • Describe the purpose and use of each pane in the RStudio IDE.
  • Understand the differences between errors, warnings, and messages in R.
  • Seek for help.

 

1.1 Installing R and RStudio

R is a free open-source statistical programming language (an implementation of the S programming language) and a powerful graphics engine, which was created by Ross Ihahka and Robert Gentleman at the University of Auckland in 1993.

RStudio is an integrated development environment (IDE) that was founded by J.J. Allaire in 2009. Today, RStudio is an open source Posit product that provides a friendly interface by adding a plenty of great features, auto-complete functions and a set of useful tools.

Throughout this textbook we will use R via RStudio IDE. Both programs can be downloaded from posit.

1.2 Starting R & RStudio

After the R and RStudio installation is finished, we click the RStudio icon .

R starts automatically when we open RStudio. The first time we initiate an R session we will see three panes (Figure 1.1):

Figure 1.1: RStudio Screenshot with three panes.

The three main panes that divide the screen are:

  1. the large Console pane on the left runs R code immediately. It is also known as the command line pane.

  2. the Environment pane, which includes among others the Global Environment (Workspace) and History tabs, in the upper right.

    • The Environment tab keeps track of the objects we create as we work with R.

    • The History tab saves all of the commands that we have sent to the console in the R session.

  3. the Output pane in the lower right which includes:

    • The Files tab allows us create new folders (directories) on our computer, as well as copy, move, delete, or rename files.

    • The Plots tab display static graphs which are generated from our data and during the data analysis. There are backward and forward arrows for navigating between previously and currently generated plots. Clicking the broom icon will clear all temporary plots from the tab.

    • The Packages tab lists of all the R packages installed on our computer and indicates whether or not they are currently loaded. We’ll discuss packages in more detail in .

    • The Help tab, displays the results of the search for R documentation.

    • The Viewer tab in RStudio allows us to view local web content (e.g., html tables or interactive htmlwidgets like plotly graphs).

    • The Presentation tab is used to display HTML slides generated via Quarto’s revealjs format.

Throughout this textbook, we’ll come to learn what purpose each of these panes serves.

Command prompt  >

The Console pane starts with information about the version number, license and contributors, and provides some guidance on how to get help. The last line is a standard command prompt (the greater than sign > symbol) that indicates R is ready and expecting instructions to do something.

 

Let’s type 14 + 16 in front of the R command prompt in the Console and press Enter :

14 + 16
[1] 30

We observe in the console that the output is [1] 30. It’s clear that 30 is the answer to the mathematical calculation of 14 + 16. However, what does the [1] mean? At this point we can ignore it, but technically it refers to the index of the first item on each line. (In some cases R prints out many lines as an output. The number inside the square brackets is an index that helps us find where in the sequence we are per line).

1.3 Errors, warnings, and messages in R

Let’s type the the word hello in the Console and press Enter:

hello

We get the following error:
Error: object ‘hello’ not found

R will show in the Console pane that something unusual is happening in three different situations:

  • Errors: When there is an error, the execution of code will stop and some relative information is reported for this failure.

  • Warnings: When there is a signal of a warning, the code will still work, but with some possible issues.

  • Messages: In many cases, messages are attached to the output after the code execution that might be useful information for the user.

Now, let’s type in the Console the following:

14 + 16 -
+

If an R command is not complete then a plus sign (+) (prompt) appears on second and subsequent lines in the Console until the command syntax is correct. In our example, we can type a number to finish the mathematical expression we are trying to calculate. We can also press the escape key Esc to cancel the command.

Recall a previously typed command in Console

In Console to go between previously typed commands use the up (\(\uparrow\)) and down arrow (\(\downarrow\)) keys. To modify or correct a command use the left (\(\leftarrow\)) and right arrow (\(\rightarrow\)) keys.

1.4 R Help resources

Before asking others for help, we should try to solve the R programming problems on our own. Therefore, it is recommended to learn how to use R’s built-in help system.

First, we can use the help() command or the ? help operator which provide access to the R documentation pages in the standard R distribution. For example, if we want information for the median we will type the following command:

help(median)
?median

RStudio also provides a search box in the “Help” tab (Figure 1.2):

Figure 1.2: The help() command searches for specific term such as “median” in the standard R distribution.

Two question marks (??) will search the help system for documentation matching a phrase or term in our R library and it is a shortcut to help.search() command. So for example, let’s say we want to search documentation specifically for the geometric median. Keep in mind if our phrase is a string, we must include it in (double or single) quotation marks.

help.search("geometric median")
??"geometric median"

To find all the names containing the pattern we search in the current R session, by partial matching, we can use the apropos() command. For example:

apropos("med")
[1] "elNamed"        "elNamed<-"      "median"         "median.default"
[5] "medpolish"      "runmed"        

Use the example() command to run the examples that are provided in the R documentation:

example(median)

median> median(1:4)                # = 2.5 [even number]
[1] 2.5

median> median(c(1:3, 100, 1000))  # = 3 [odd, robust]
[1] 3

Additionally, there are a lot of on-line resources that can help (e.g., RSeek.Org, R-bloggers, Stack Overflow). However, we must understand that blindly copying/pasting code could produce many unexpected code bugs and further it won’t help to the development of our programming skills.