Install R / R Studio

Install R Installing R. This lesson is short, but you will need internet as we are downloading the main software.

Websites that you will need:

R-Project: www.r-project.org

CRAN: http://cran.r-project.org

First, install R. Go to  www.r-project.org

You should get something like this. Click on the download R link (in blue). A mirror is just a copy of a webserver, you should choose one close to you geographically, but it doesn’t really matter. There are two in South Africa.

Once you choose a mirror you will be redirected, and your page should look something like this:

Download the version appropriate for your operating system (just follow instructions). If you are asked you want to say YES to install for all users and YES to make a local library. If you are not comfortable finding software on your computer, it probably also makes sense to say YES to making a shortcut.

Congratulations, R is installed. If you open it you should get the back box. What version of R have you installed? You can see that mine was 3.4.1 “Single Candle”.

We will use R with RStudio, so just go to the next lesson when this is done.

INSTALL RStudio RStudio is not R, but it makes working with R much easier. RStudio is what is called a GUI or ‘graphical user interface’, but it has a lot of convenient helper bits, and so the rest of this course will use RStudio to interface with R. Go to www.rstudio.com and download the free RStudio Desktop version appropriate for your OS (operating system), just follow instructions.

When you open RStudio, you should get something that looks approximately like this (keep in mind that I run a Linux computer, so some small things will be different):

It’s worth spending some time playing with the menus and buttons to see what there is available. Usually you will see RStudio with four windows, like you see below.

Top left: the R script file (this is a plain text file with a .R ending that you can use to save your code)

Bottom left: called the console this is R. You can type, compute and calculate directly in the box.

Top right: In this case the box contains a tab called History which usefully contains a history of all of the commands you sent to the Console and a tab (visible) called Environment. When you have done some work and named some data frames you will see the environment lists all of the R objects as well as some of their attributes (like dimension).

Bottom right: This has a whole bunch of helper tabs include a the directory that R thinks is the workspace, a tab for viewing Plots, and the all important Help.

If you don’t like the arrangement of the windows, or you would like a different colour scheme you can change these in the Menu > Tools > Global Options.

Libraries, packages, lions, bears, oh my!

Getting a handle on packages, libraries, and the confusing way they are the same thing is critical to having a good and happy time working with R. The basic installation of R is just that – basic. Much of the excellent functionality and graphics and advanced methods are held in packages. In order to use these great functions, you need to install and load the relevant files.

You need to INSTALL PACKAGES ONLY ONCE

This is how.

The pointy-clicky way:

  1. Open R Studio
  2. Go to Tools > Install Packages

You need to type the name of the (caSE MaTTeRS) of the package you want to install in the space. Leave everything else as default. CRAN is the place where most of the R packages live. It’s a good place.  Then click Install – you will see some stuff written to the console. And that’s it. Nothing else to see here… Remember, you only need to do this once, so it doesn’t matter so much if you learn how to do it the super-start coder way (below) or not.

The superstar coder way:

Type into console something like this:

install.packages(“package_name”)

In this example, I installed the package “epiR” (remember: caSE matters; also epiR is a super useful package for budding epidemiologists), and then some stuff happens and then nothing. This is exactly the good thing to happen. You won’t be able to do anything with the great functions in epiR until you load the library though (below).

You only need to do this one time (per computer). Then the package is available.

Problems:

RStudio might ask you things about all users, or paths, or libraries, just say yes yes, and yes.

Where do you find packages?

Start here: https://cran.r-project.org/web/views/

You need to LOAD LIBRARIES EVERY TIME but it will only work after you have installed the package. 🙂

Type into the console:

library(“package_name”)

It might request, or load, additional packages – that’s ok – say yes.

Every time you work in R, you will need to load your libraries. Usually we start our script files with these sets of commands. Script files are for another lesson.