R - the basics

R

R is a software programming language fordata manipulation, simulation, statistics, and graphics. It has become the main tool among statisticians, researchers, and data scientists.

  • R is FREE, open-source software. Youcan download it on any computer

  • State of the art: Data scientist provide their methods as R packages.

  • Excellent graphics: if you can draw it on paper, you can craft it in R.

  • The software, including all the corresponding specialty packages, are updated regularly (several times a year) as new statistical techniques are developed.

You will not master R overnight, as you are learning a new language. You will make errors, deal with frustration, but with practice it will all become easier.

Additional R Resources

Supplemental R reference material is widely available on the web.

AI
Not all LLMs are equal, use the full version of Gemini (the UofA gives you access to this for free) or Claude (paid). Ask it to provide code, explain code and solve errors.

RStudio Cheat-sheets: https://www.rstudio.com/resources/cheatsheets/
Print and use as additional reference material.

The R Project website: http://www.r-project.org/
A good resource to get started. The website provides a list of manuals is available for free download.

Quick-R : http://www.statmethods.net/
A great google-style resource for almost anything with clear examples.

Cookbook for R http://www.cookbook-r.com/
A good resource for simple reminders.

Additionally, there are many guides online. Throughout this course we will use some of them.

Getting Started in R

1. Installing R on Your Computer

R is free, open source software that can be easily downloaded from The R Project website: http://www.r-project.org/

  1. Click CRAN (The Comprehensive R Archive Network) on the left-hand side of the website.

  2. Select the CRAN Mirror you want to reference.
    Generally, select the CRAN Mirror closest to your geographic location.

  3. Select the R platform that your computer is running (e.g. Windows, Mac OS X, or Linux); select “base”, this will install the base package.

  4. Download the latest version of R and install.

A note on updating R

Developers update the R software several times a year. However, as R is open-sourced, the R software is updated separately from the packages. In some cases, packages that were built under a previous version of R may not be compatible with the latest version of the R software. You can download the previous version of R which the package is built under from the R archives and run your analysis from that version.

RStudio

When you open R you will see the R Gui (]graphical user interface, pronounced “gooey”).

You can start working in R at this point by typing commands into the “R Console”, however you will quickly learn that simply using R in this form is not efficient. We will use RStudio instead of the standard R GUI. RStudio is an Integrated Development Environment (IDE) for R that allows you to interact with R more easily.

Install RStudio:

To use RStudio, you first need to have R installed on your computer.

  1. Download the software from the RStudio website: https://www.rstudio.com/
    Download the free desktop open source license (some of the other options cost $). Similar to the R download, you can select the download required for your operating system. Follow the install pop-up windows to complete the installation.

  2. Open RStudio
    You do not have to have R open or running to open RStudio. As long as it is available on your computer, RStudio can access it.

RStudio Projects

In RStudio working directories are called “Projects”. For each project you are working on, create an R-Project

  1. File > New Project.
    Typically you will create a new working directories for a new project.

  2. New directory > New Project

  3. Enter the name for your directory/ project
    Select browse, navigate to the file path where you want your directory housed.

  4. To finish, select create project. RStudio will create a folder reflecting the directory name you entered in the file path you selected.

Once created the project acts like the working directory reference for data importing and exporting, however you can still use the setwd() function in your code if you would like to change the reference. You can see the active working directory file path at the top of the file tab.

Using R-Markdown in RStudio:

The first time you open RStudio you will see 3 windows. The fourth window is your script page, which is hidden by default. We will use R Markdown, as it allows you to mix code and text.

  • Select File > New File > R Markdown

In Markdown, you can write notes, explanations etc in between blocks of codes. You can insert code using the green +C button in top menu bar

You can have multiple files open (in different tabs) but you can only have one Project open in R

Packages in RStudio

R itself has some functionality, through its built-in package, Base. The true functionality of R comes from user-developed packages. There are thousands of packages available on CRAN, for RNA sequencing, flow cytometry, making graphs, manipulation of data tables, etc.. This huge variety of packages is one of the reasons why R is so popular: the chances are great that someone has already solved a problem that you’re working on, and you can benefit from their work by downloading their package.

RStudio lists all the packages you have installed in the Packages Tab in the right bottom window. If the package is active there will be a check mark next to the package.

You can “activate” packages in two ways, by checking them in the list, or by using the library() function.

To install packages that are not pre-loaded, you can use the function install.packages() to download the package.

Optional: Setting up up your interface

In RStudio, there are multiple windows called “panels” each with specific attributes. You can customize the location of the windows to our preference.

  1. Select Tools > Global Options

  2. Under General, set your standard working directory. For individual projects, you will be able to set a new working directory.

  3. Under Pane Layout (left hand side) you can use the drop-down field to select the content you would like in each of the 4 panes. Note that Source will contain your script file.