This is a short tutorial on how to use R.
This format indicates that the text is R code:
2 + 16
This format indicates that the text is the output of running some code:
## [1] 18
First, you need to download and install R (from here, I’m using version 3.2.2, but you should install the latest version, or at least check you have version 3 or above). This will install the R language and a basic interface for using R.
We’ll also be using an advanced interface for R called "RStudio Desktop". This also needs to be downloaded and installed (download it here. Make sure you do this after installing R.
The installation should be as simple as downloading the installers and running them. Here are some Youtube videos that show you the process:
When you have installed both R and RStudio, open Rstudio. The program looks like this (the position of each window might be different for you, and you might have a newer version of RStudio, but it will look something like this):
When using R studio, you’ll write code in your script window and execute code in the console.
The script is just a text file that you can save on your computer. The idea is that you (or someone else) can open this up and run all the commands in it to reproduce a particular graph or analysis. Think of it like the script to a theatrical play: a list of all the steps that are taken in your analysis.
The console is where the commands are actually executed. Think of this as the “stage” where the live performance happens. You can also use it to test out a line of code as you write it, or check the contents of a variable (maybe like a read-through of a theatrical scene to check if it works, but that might be taking the metaphor too far). Anything you type here will not be saved when you restart RStudio, so to stop you losing progress it’s always best to write code in the script first (where it can be saved), then run each line in the console from there.
By default, the console is in the bottom left of the screen - the window with an “>” symbol, waiting for you to give input.
On the top left is the script window. You can write R scripts - text files with lists of R code - here and them save them for later. You can pass lines of code from this script to the console by hilighting some text and pressing the “Run” button in the top right (or type Control+Enter on Windows, or Command+Enter on a Mac).
On the bottom right is a space for displaying plots and accessing the help files. On the top right there is a space for displaying what objects are in the memory at the moment. I don’t tend to use this window much, so you can minimise it if you like.
For a basic introduction to the RStudio interface, you could check out a short youtube video like this one.
Make sure that the RStudio environment is comfortable for you. You could:
Adjust the size of text
Adjust the colours
Adjust the position of the panes
So that it looks like this:
FEEL like an AWESOME HACKER
Please listen to either Yoko Kanno or your favourite Glitch Hop band.
Go to the next tutorial
Back to the index