3.5 Getting started with RStudio

Now we’ll get started with RStudio. We’ll get to know what it looks like and configure it a bit for out needs.

3.5.1 RStudio at first glance

The RStudio logo looks like this.
The RStudio logo

(#fig:rstudio.logo)The RStudio logo

When you open up you’ll be greeted by a fairly busy array of menus and things. Don’t panic! A typical fresh starting point in RStudio is shown in Figure 2.

RStudio when first opened.

(#fig:rstudio.open)RStudio when first opened.

When referring to RStudio, there are two terms that need to be understood. As shown in Figure 3, there is the console section of RStudio and the script editor or source viewer.

RStudio's console and script editor.

(#fig:rstudio.console)RStudio’s console and script editor.

A “cheat sheet” called the “RStudio IDE Cheat Sheet” details all of RStudio’s many features and is available at https://www.rstudio.com/resources/cheatsheets/ . It very thorough, though a bit dense.

3.5.2 The console versus the script editor

You can type and enter text into both the console and the script editor. The console, however, respond like a calculator, while the script editor works more like a text editor.

3.5.2.1 The R console

The console in RStudio act exactly the same way as it does in R. This is an interactive programming situation that is very similar to a scientific calculator. If you click your mouse inside the console and type “1 + 1” then press enter you will see the following type of output

1 + 1
## [1] 2

Note that right in front of where you typed “1+1” there is a “>” symbol. This is always in the R console and never needs to be typed.

One thing to note about R is that it’s not particular about spacing. All of the following things will yield the same results

1+1
1 + 1
1          +        1

3.5.2.2 R’s console as a scientific calculator

You can interact with R’s console similar to a scientific calculator. For example, you can use parentheses to set up mathematical statements like

5*(1+1)
## [1] 10

Note however that you have to be explicit about multiplication. If you try the following it won’t work.

5(1+1)

R also has built-in functions that work similar to what you might have used in Excel. For example, in Excel you can calculate the average of a set of numbers by typing “=average(1,2,3)” into a cell. R can do the same thing except

  • The command is “mean”
  • You don’t start with “=”
  • You have to package up the numbers like what is shown below using “c(…)”
mean(c(1,2,3))
## [1] 2

Where “c(…)” packages up the numbers the way the mean() function wants to see them.

If you just do the following R will give you an answer, but its the wrong one

mean(1,2,3)

This is a common issue with R – and many programs, really – it won’t always tell you when somethind didn’t go as planned. This is because it doesn’t know something didn’t go as planned; you have to learn the rules R plays by.

3.5.2.3 Practice: math in the console

See if you can reproduce the following results

Division

10/3
## [1] 3.333333

The standard deviation

sd(c(5,10,15)) # note the use of "c(...)"
## [1] 5

3.5.2.4 The script editor

While you can interact with R directly within the console, the standard way to work in R is to write what are known as scripts. These are computer code instructions written to R in a script file. These are save with the extension .R but area really just a form of plain text file.

To work with scripts, what you do is type commands in the script editor, then tell R to excute the command. This can be done several ways.

First, you tell RStudio the line of code you want to run by either * Placing the cursor at the end a line of code, OR * Clicking and dragging over the code you want to run in order highlight it.

Second, you tell RStudio to run the code by * Clicking the “Run” icon in the upper right hand side of the script editor (a grey box with a green error emerging from it) * pressing the control key (“ctrl)” and then then enter key on the keyboard

The code you’ve chosen to run will be sent by RStudio from the script editor over to the console. The console will show you both the code and then the output.

You can run several lines of code if you want; the console will run a line, print the output, and then run the next line. First I’ll use the command mean(), and then the command sd() for the standard deviation:

mean(c(1,2,3))
## [1] 2
sd(c(1,2,3))
## [1] 1

3.5.2.5 Comments

One of the reasons we use script files is that we can combine R code with comments that tell us what the R code is doing. Comments are preceded by the hashtag symbol #. Frequently we’ll write code like this:

#The mean of 3 numbers
mean(c(1,2,3))

If you highlight all of this code (including the comment) and then click on “run”, you’ll see that RStudio sends all of the code over console.

## [1] 2

Comments can also be placed at the end of a line of code

mean(c(1,2,3)) #Note  the use of c(...)

Sometimes we write code and then don’t want R to run it. We can prevent R from executing the code even if its sent to the console by putting a “#” infront of the code.

If I run this code, I will get just the mean but not the sd.

mean(c(1,2,3))
#sd(c(1,2,3))

Doing this is called commenting out a line of code.