Welcome to our first computer lab session together! Today, we will introduce you to a programme called R, which is an open-source statistical software. The main aim of the first lab-session is to familiarize yourself with R, using a built-in tutorial, and in the second session, we will apply and expand what you have learned using data on the EU referendum and text data on referendum-related speeches by the party leaders.
For both labs, we will work in pairs (or small groups of 3). You can choose to all run the code on your own computers or all gather around one person’s computer. This worksheet is available in electronic format here. By looking at it online, you can copy and paste the commands where appropriate.
Begin by opening RStudio (located on the desktop). Your first task is to create a new script (this is where we will write our commands). To do so, click:
File --> NewFile --> RScript
Your screen should now have four panes:
the Script (top left)
the Console (bottom left)
the Environment/History (top right)
Files/Plots/Packages/Help/Viewer (bottom right)
The Sc ript (top left) is where we write our commands for R. Let’s try writing your first command. Type the following command into your script (editing the message as you like)!
x <- "My message goes here" #Note the quotation marks!
To tell R to run the command, highlight the relevant row in your script and click the Run button (top right of the Script) - or hold down ctrl+enter or cmd+enter on Mac - to send the command to the Console (bottom left), where the actual evaluation and calculations are taking place.
Running the command above creates an object named ‘x’, that contains the words of your message. It is good practice to name your objects so you can later refer back to them!
You can now see ‘x’ in the Environment (top right). To view what is contained in x, type in the Console (bottom left):
x
R is basically a very powerful calculator. But you can use it to do very simple arithmetic operations as well. In your console, type:
3+3
3-3
3*3
3/3
Often, the ‘basic’ version of R will not be able to do more advanced analyses or make nicer plots. This is when we will need to install a so-called ‘package’ - a program that includes new tools (i.e. functions) to achieve specific goals. You can think of them as ‘extensions’ enhancing R’s capacities. For instance, the package “ggplot2” contains functions to create a variety of graphs and figures, the package “maps” contains functions to create maps.
You can load the most recent version of packages using RStudio (by clicking Install under the packages tab of the bottom right pane and typing the name of your desired package) and ensuring the top drop down menu says install from Repository (CRAN). Keep Install dependencies ticked. This is the way you should load the package we will use in this session, swirl. You could also type into your console directly the following command:
#For example:
install.packages("swirl")
install.packages("quanteda") #We will this package in the second session
After you have installed a package, you will need to load it in order to be able to use it. This is done by typing library(
library(swirl) #Note that this code does not include quotation marks around the name of the package!
We will now open and use the swirl package, which contains built-in, interactive R tutorials. In these tutorials, you will be asked questions in the console by the computer (in red). Note that, when doing your own programming in R, it will not tell you what to do and which commands to use. Therefore, we recommend that everytime you encounter a new command in the following exercise to write it down in your R-script. Now let’s gets started!
Remember from above, we first need to load the package before we can use it. The first line of the code below does exactly that and the second line prompts swirl into action.
library(swirl) #loads the package
swirl() #starts up swirl and allows you to select from installed courses
You will now be asked by the Swirl package in the console to enter your name. Follow the subsequent instructions until you get to a menu where you can choose between 4 different courses. Then, select “R Programming: The basics of programming in R” by typing 1 when prompted. Now use the R Programming lessons to practice using R; we suggest having a go at the following lessons:
Basic Building Blocks
Vectors
Matrices and Data Frames
Base Graphics
If you are on a computer at home, you can load R to continue working on some of the examples from the lab and try out some new ideas.
All three programs are free. Make sure to load everything listed above for your operating system or R will not work properly! Once you have loaded the programs above, you can open and use RStudio (you do not need to separately open XQuartz or R).
Saving scripts: To save your script in RStudio (i.e. the top left panel), all you need to do is click File –> Save As (and choose a name for your script). Your script will be something like: myfilename.R.
Saving plots: If you have made any plots you would like to save, click Export (in the plotting pane) and choose a relevant file extension (e.g. .png, .pdf, etc.) and size.
To save individual objects (for example x from above) from your environment, run the following command (choosing a suitable filename):
save(x,file="myobject.RData")
load(file="myobject.RData")
save.image(file="myfilname.RData")
load(file="myfilename.RData")
If you are at home, when you open a new script make sure to check and set your working directory (i.e. the folder where the files you create will be saved). To check your working directory use the getwd() command (type it into the Console or write it in your script in the Source Editor):
getwd()
To set your working directory, run the following command, substituting the file directory of your choice. Remember that anything following the `#’ symbol is simply a clarifying comment and R will not process it.
## Example for Mac
setwd("/Users/Documents/mydir/")
## Example for PC
setwd("c:/docs/mydir")