1.1 Start using R!

1.1.1 Install R and Rstudio

To begin, you should download and install R from the CRAN. This is the online hub for the R language and it stands for Comprehensive R Archive Network. Be sure to download the correct R installation for your operating system.

We also strongly recommend you install RStudio, a front-end for R. This utility makes working in the R environment a lot more straightforward, standardises things across operating systems and has many helpful features. For the purposes of these tutorials, we will assume you are using RStudio.

With both R and RStudio installed, start Rstudio and we will begin!

1.1.2 Getting familiar with R and the console

1.1.2.1 R as a calculator

In the bottom left you will see a > where you can input text. This is called the console, and you can directly interact with R through this. Try inputting a number, or any calculation you can think of, and then press enter. For example:

4
4+3
11-5
5*2
3/2
10^2

You will see that if you input a single number, R will return that same number to you. If you input a calculation, R will return the result of that calculation.

You can also use parentheses as you would on a normal scientific calculator:

10*3+4
#> [1] 34
10*(3+4)
#> [1] 70

You have now learned how R can be used as a calculator!

1.1.2.2 Text input

If you input text into the console, however, you will get the following:

hello
#> Error in eval(expr, envir, enclos): object 'hello' not found

The error message reason for this will be apparent later in this tutorial. To get R to return text, you have to enclose the text in single or double quotes (" or ') like so:

"hello"
#> [1] "hello"
'hello'
#> [1] "hello"

Important concept: To make R interpret what you write as text, you have to enclose it with single or double quotes:

  • "hello" or 'hello' rather than just hello

Error messages:
Above, you got an error message saying “Error: object ‘hello’ not found”. Errors are common in R, and are nothing to be afraid of. The worst thing that can happen from an error in R is that you have to fix your code and re-run it. You should get used to reading error messages, they are often helpful to understand what is wrong with your code. They can also be extremely unhelpful, but even then, googling the error message can give you some insight into what went wrong.

1.1.2.3 Comments

If you input a #, R will ignore whatever comes after on the same line. This means you can write entire lines of comments, or write comments after your calculations:

# This entire line is a comment, and that is fine!

6 + 4 # add a meaningful comment on why you're doing this

Comment your code liberally, so it becomes easier to understand for any person reading your code. That person is often your future self, and you’d be surprised to learn how unreadable uncommented code can be, even if you wrote it yourself! Fortunately, you will never end up in that situation, since from now on you will be commenting everything you do (because I said so).

1.1.2.4 Some additional tips about the console

  • use the up and down arrows to cycle through your previous commands
  • if the > in the console turns into a +, it means that you probably forgot to close a parenthesis or a quote (try e.g. running "hello). You will be unable to do anything while the console is behaving like this. Press the Esc button to get your familiar > back and continue working.
  • Spaces generally don’t mean anything when doing calculations, which means 4+3 is equivalent with 4 + 3. Use spaces to make your code easier to read.

1.1.3 Scripts

Doing small operations in the console is all well and good, but once you’ve done something a bit more complex than adding numbers you will want to save what you did in some way. You could use the up-arrow in the console to recall the things you did (even if you close and reopen RStudio), but imagine if the thing you want to recall was a thousand operations ago. That’s a whole lot of button pressing! The most convenient way to store what you have done in R is by writing a script.

A script in R is just a text document containing R code, and the file extension is .R (your script can e.g. be called “myscript.R”. In RStudio, you can create a new script either from the menu File > New File > R script or by pressing ctrl+shift+N on Windows or command+shift+N on Mac. To save your script, use ctrl/command+S, or choose save/save as... from the File dropdown menu. Be concious about where you save your script so that you will find it again later!

Working in a script is almost exactly the same as working in the console: You write a line of code in the script, run it, and the result is shown in the console. The main difference is that you have to press ctrl/command+enter to run code from a script, rather than just enter. The other big difference is that you can save your scripts, so you have access to all the code you have previously written. For this reason, you should always work in a script rather than in the console.

Exercise: Create a new script, and save it with a meaningful name (e.g. “BIOS1140_week01.R” in a folder named BIOS1140). Run the commands you ran in the console earlier, but this time from your script instead. Remember to comment your code with # as you go along!

Whenever you start a new project, create a script for that project. Save your script in a meaningful location with a meaningful name so it will be easy to find later.