## 2. A first R session

In this section you’ll be introducted to the basic building blocks of R and R-studio.

Once more: no experience with R, programming nor statistics is required.

Section structure:

• 2.1. Introduction to the R-studio interface
• 2.2. Executing commands
• 2.3. Creating objects
• 2.4. Saving your R script

At the end of this section, you’ll be able to perform simple computations using object-oriented programming (doesn’t that sound fancy?).

#### 2.1. Introduction to the R-studio interface

Clearly, the first step is to start the R-studio program.

The R-studio interface consists of four ‘panels’ (if you happen to see only three panels, please select File > New File > R Script). We’ll focus on the two panels on the left of the screen: These are called the “editor window” and the “console window”.

• In the editor window you need to type “commands” in the R language: this way you tell R what you want it to do (input).

• The console window: this is where R tells you what is has done with your commands (output)

#### 2.2. Executing commands

We are now going to practice executing a simple command in R. Type in the editor window:

3 * 5

To execute this command (that is, tell R that you want it to calculate 3 times 5), you need to place your cursor in the editor window next to the typed command. Left-click on the button located on the upper-right corner of the editor window to execute (‘run’) it. Alternatively, you may use keyboard shortcut: Ctrl + Enter (on windows) or Cmd + Enter (on Mac) to execute the command. When successful, the console window should show:

> 3 * 5
[1] 15

Notice that the console window first returns the executed code (3 * 5) and subsequently the result of the requested computation (15). For now, you can ignore the [1] in the output.

Executing commands line-by-line can become tedious. To run multiple commands at once, you can select the lines to be executed and again use the run-button (or the keyboard shortcuts Ctrl + R or Cmd + Enter). To try this: type in the editor window the command: 8 / 4 on a new line. The editor window should now display two separate commands:

3 * 5
8 / 4

After executing the commands, the console window should look like this:

> 3 * 5
[1] 15
> 8 / 4
[1] 2

In the remainder of this tutorial, commands will be displayed in a box with grey background and results will be displayed with transparant background as follows:

3 * 5
[1] 15

#### 2.3. Creating objects

R is a so-called object-oriented programming language. In practice, this simply means that it works with “objects”. An object can be thought of as a container with a name that contains information that you assign to it. To create an object, start by typing the name for the object. After specifying the name, we write <-, followed by the content of the object. Suppose we want to create an object called ‘result’ and we want to assign to it the numeric result of 10 * 2. Type in the editor window:

result <- 10 * 2

and run this line. Notice that the console window does not display the result of the computation 10 * 2 stored in the newly created object (the content). To also display the content of the object, type: result on a new line in the editor window and run this new line. The console window should display:

[1] 20

Admittedly, in this simple computational example working with objects is impractical. Please bear with us, objects will prove to be very helpful later in this tutorial!

Now, type: result - 1 on a new line in the editor window and run this new line. The console window should display:

[1] 19

Store the new computation in a new objects called result2. Type in the editor window:

result2 <- result - 1
result2

and run these lines.

Notice that the objects result and result2 that you just created are listed in the environment window in the upper-right panel of the R-studio interface.

Finally, and please remember this, R is case-sensitive. This means that result, Result and RESULT will all be considered different objects in R! It is good practice to avoid using capital letters! Really, this will prevent you from experiencing some unnecessary frustrations!

#### 2.4. Saving your R script

In this section we asked you to type some commands in the editor window. The combination of these different lines of code is called an “R script”. We advise you to save your R scripts regularly (as you should for any document). To save your R script, go to File > Save As… (or File > Save). By default, the script will be saved using an .R extension (a “dot R file”).