Practical 1

Requirements

  • R and RStudio must be installed prior to the session - you can follow the instructions below

Lecture

Lab

Materials for this practical can be downloaded from here.

1. R basics

In the first module of the workshop, the goals are to familiarize with the language and the logic behind it; Get started with R studio and create your first project; Create your first .R file to write down the live code; and get comfortabble with installing packages.

Module content: - Seeking help - Installing packages

Script

2. Data types: attributes and built-in functions

In this section participants will understand the differences between classes, objects and data types in R; Compute arithmetic operations; Use logical operators; create objects of different types, learn about their attributes and apply some built-in functions in R; Subset and index objects; and get comfortable with vectorized operations.

Module content: - Vectors - Lists - Factors - Data frames - Arrays - Coercion

Script

3. Control structures

In this module, participants learn to use if-else, while, and for loops; as well as building their own functions; and what packages are.

Module Content * If..Else * While loop * For loop * Functions * Packages

Script

4. Basic data manipulation

In this module participants will learn how to read/write data to/from files with different formats (.tsv, .csv); become familiar with basic data-frame operations; (3) index and subset data frames using base R; manipulate individual data frame columns.

Module content: - Reading/writing data - Exploring data frames

Script

5. Advanced data manipulation

The fourth module participants will familiarize with the dplyr syntax; create pipes with the operator %>%; perform operations on data frames using dplyr and tidyr functions; learn how to join columns and rows of different data frames; and implement functions from external packages by reading their documentation in R.

Module content: - Handling data frames with dplyr - Other useful packages - Hands-on: advanced data manipulation

Script

6. Generating visual outputs

This section will show participants how to (1) Create basic plots using base R functions; (2) Understand how to connect data frames with ggplot2; (3) create basic graphs with ggplot2; (4) use factors to customize graphics in ggplot2; (5) use RMarkdown to generate customized reports.

Module content: - Figures with base R - Graphics with ggplot2

Script

Repo structure

├── README.md
├── .gitignore
├── _config.yaml
├── IntroToR.Rproj
├── Exercises
│   └── README.md
│   ├── data
│   │   └── co2_pcap_cons.csv
│   │   └── penguins.csv
│   ├── scripts
│   │   └── 01GettingStarted.Rmd and .html
│   │   └── 02DataTypes.Rmd and .html
│   │   └── 03ControlStructuresAndFunctions.Rmd and .html
│   │   └── 04BasicDataManipulation.Rmd and .html
│   │   └── 05AdvancedDataManipulation.Rmd and .html
│   │   └── 06GeneratingOutputs.Rmd and .html
├── Slides
│   └── IntroToR_CBHWorkshop.pptx
└── Outline
    └── workshopoutline.pdf #using the template provided

References

The materials for this workshop were based on the following sources: - Base R Cheat Sheet
- Google’s R Style Guide - Mastering Software Development in R

Data in this workshop comes from: - Horst AM, Hill AP, Gorman KB (2020). palmerpenguins: Palmer Archipelago (Antarctica) penguin data. R package version 0.1.0. https://allisonhorst.github.io/palmerpenguins/. doi: 10.5281/zenodo.3960218 - Gapminder data free from www.gapminder.org, CC-BY LICENSE - Bryan J (2025). gapminder: Data from Gapminder. doi:10.32614/CRAN.package.gapminder https://doi.org/10.32614/CRAN.package.gapminder, R package version 1.0.1, https://CRAN.R-project.org/package=gapminder. - Higgins P (2021). medicaldata: Data Package for Medical Datasets. doi:10.32614/CRAN.package.medicaldata https://doi.org/10.32614/CRAN.package.medicaldata, R package version 0.2.0, https://CRAN.R-project.org/package=medicaldata.

Workshop created as part of the McGill Initiative in Computational Medicine