1 Introduction to the pre-sessional for R
The R pre-sessional workshops are designed to help you get started with R which you will need for some modules during your degree programme. For example, students who will take a Methodology module will likely be using R in those seminars.
This webpage contains information on:
- Instructions on how to install R onto your laptop
- Why would we want to learn R
- The pre-sessional materials
- Getting started instructions
1.1 Information on the workshops
The pre-sessional workshops are two hour long, in-person practical workshops to help you learn R. You will be working through practical exercises, designed to build your knowledge in R, while being supported by a team of data science trainers.
The content for these workshops will cover the key fundamental knowledge needed for using the R programming language. The content is split into worksheets which gradually progress you through topics.
Completing all the worksheets will take around 15 hours to complete. Completing the fundamentals materials will take around 7 hours.
These topics include, but are not limited to:
Working with different data types in R such as numerical, text, categorical, data frames (spreadsheet like data structures), matrices, and lists
Learning to manipulate these data types to extract what information you need, and perform operations on them
Using conditional operations to manipulate data
Using functions, built in code, to perform a wide array of operations like finding the mean
Loading datasets into R, and handling some common issues
Using external packages, such as the popular tidyverse set of packages, to subset, modify, and aggregate your data
Create an array of data visualisations to understand your data better
Automating code, using tools like a for loop
Streamline your code by writing your own functions
Below is a diagramme showing how all the worksheets interact which should help you know where to start, and where you are going.
2 Installing R and RStudio
Below are step by step instructions for getting the R and RStudio installed on your own laptop.
2.1 Windows install
2.1.1 Install R
To install R, you need to download the installer from the R website
Click on either base or install R for the first time
Click on the download R for Windows link
Once downloaded, open the
.exe
file and follow the installation instructions on your computer
2.1.2 Install RStudio
To install RStudio we download it from the Posit website
Click on the Download RStudio Desktop link
Once downloaded, open the
.exe
file and follow the installation instructions on your computer
2.2 Mac install
2.2.1 Install R
To install R on your Mac you need to know the type of processor your Mac uses. This is straightforward to find out:
- On the top navigation bar on your Mac, click on the apple icon
- From the drop down menu, select About This Mac
- In Overview you will find the information about your Mac. If you have an Intel Mac, you will see the processor row, which has information that includes Intel. If you have an M-Series Mac (M1, M2, M3, M4 etc. chips), you will see chip and M1/M2/M3/M4 in the Overview with something like Chip Apple M1
2.2.1.1 M-Series Mac (M1, M2, M3, M4 etc. chips)
To install R, you need to download the installer from the R website
If you have a M1 Mac you will need click on the link the contains arm64 to download R. It will look something like
R-4.4.1-arm64.pkg
Once downloaded, open the
.pkg
file and follow the installation instructions
2.2.1.2 Intel Mac
To install R, you need to download the installer from the R website
If you have a Intel Mac you will need to click on the link that just contains the version of R. It will look something like
R-4.4.1.pkg
, and can be located a touch further down the page under the header Binaries for legacy macOS/OS X systems:Once downloaded, open the
.pkg
file and follow the installation instructions
2.2.2 Install XQuartz
To run R on a Mac operating system, XQuartz is required. You can install it by following this link, downloading it and following the installation instructions.
2.2.3 Install RStudio
To install RStudio we download it from the Posit website
Click on the Download RStudio Desktop link
Once downloaded, open the
.dmg
file and follow the installation instructions on your computer
2.3 Installing R and RStudio installation issues
If your installation for R and RStudio did not work, this is likely because your computer is running an older operating system. In these cases you have a few options available to you.
The first option is to install an older version of R or RStudio that matches the operating system you are using. These older version can be found on the R and RStudio installation pages. The second option is to use Posit Cloud, which is a cloud based version of RStudio.
Once you have signed up you will need to start a new project. Select New RStudio Project. See the image below for help.
You will then have RStudio available to you in the cloud! You will need to upload the Rmd files to be able to use them. Click on the upload button in the bottom right panel, then choose a file. Note you will need to have downloaded the materials first, see section 4 for this! See the image below for help.
2.4 Note on R and RStudio
You might be asking yourself, why have I installed R and RStudio? Or why am I using RStudio and not just R?
An abridged answer to this question is R is the language we will be using, and RStudio is the environment in which we will be using R.
The unabridged answer is that R is a computer language, which means it cannot be opened like other computer applications such as an internet explorer (Chrome or Firefox) or Microsoft Word. Instead, to use R you write commands in the R language and ask your computer to interpret them. Previously, this would involve typing commands into a terminal application, or writing a script in a text editing software and running it through a terminal application. This is not the easiest way of writing R code, and is where RStudio comes in!
RStudio is an application, like Google Chrome or Firefox, which means it is easy to open on your computer. RStudio is a popular tool for using R, as it provides a pleasant interface for you to use R, with helpful features like the auto-completion, file management, and an environment panel to show/explore your data.
The terminal is a program that you use to type in commands that are then executed by your computer’s operating system. It is a text input/output environment.
3 Why R?
A lot of LSE courses, such as those in the Methodology Department, primarily use R. This is because R is a excellent tool for:
- Statistics
- Data handling (i.e. cleaning and manipulating data)
- Visualisations, interactive graphics, and dashboards
- Reporting (i.e. academic writing as well as building websites)
R is an open-source tool, which means you do not need to buy a licence in order to use it, and is a popular programming language, as shown in the PYPL index from 2023
Some cool things you can do in R:
This webpage was built using R. If you are interested chat to one of the instructors about it!
4 Workshop materials
4.1 Workshop format
- Each workshop is 2 hours long
- You choose which topic to work on. It is advised to work through the materials in the prescribed order unless you are already familiar with a particular topic
- You will work with fellow learners, utilising your prior experience and web searches to find solutions, with a R expert on hand if you get stuck
- Workshop sessions are hands on and practical
4.2 Download the R Markdown and data files
The files for the workshops can be downloaded from the button below:
Once downloaded it is important that you unzip/uncompress the file. The download is a zip file, which a file that has been compressed to save space for easier transfer and storage. You should uncompress these files otherwise it can be hard for some software to use the files inside.
Windows
Right-click on the file and choose Extract All, then select a location to save the uncompressed file.
Mac
Double click on the file and the uncompressed contents will appear next to your compressed file.
An R Markdown file has sections of text and code together in a single document. This is helpful for tutorials, documentation, and writing reports.
An in-depth description can be found in the R for Data Science open source book
5 Getting started with RStudio and the R Markdown files
Below are guides on how to get started using RStudio and the R Markdown files. If you prefer a video guide see Section 5.3.
5.1 Opening the R Markdown files
Opening a R Markdown file is very similar to opening a file in other software by using the menus such as File > Open File...
. R Markdown files have the extension .Rmd
.
5.2 Using a R Markdown file
We are using R Markdown notebooks for these workshops as we can add text around code, which can help us guide you. The image below shows the basic layout of a R Markdown document.
There are three ways to run code in a code cell. The two ways mentioned in the image (Run and play button), and third is to use ctrl/command + Enter
to run each line just like you would with an R script.
The most important thing for you to take note of is the code cells. Each code cell is like a small R script. You’ll notice the ``` markings around the code cell. It is important these do not get deleted, so be careful when editing your code!
Finally, you can add your own code cell using option+command+i
on a Mac or ctrl + alt + i
on a Windows machine.
Throughout the notebooks you will come across exercises, with empty code cells for you to type your code into. Your task is to test out the examples, then try and do the exercises.
The below image is a screenshot of one of the R Markdown notebooks which should guide you on how to get started using them.
You can also use the visual mode in RStudio, which can make using the notebooks easier. To do so click the tab in the top left corner of RStudio that says visual. The image below shows the visual mode.