Section 1 of 7

1 Before you start

Before you begin, take a few minutes to check what you already know and how confident you feel. You will see the same questions again at the end of the module — this helps both you and us measure what you have learned. Click an option for every question and confidence rating, then click Next to continue.

Pre-test

The file diabetes_clinic.csv is in your workspace. Which line reads it into R and stores it in an object called clinic?

Pre-test

read_csv() lives in the readr package. Which line switches that package on for the session before you use it?

Pre-test

read_csv() returns a tibble. What does the tibble printout tell you under each column name that helps you check the load?

Pre-test

You have just run clinic <- read_csv("diabetes_clinic.csv"). Which line lists every column down the page with its type and first values, to confirm it loaded correctly?

Pre-test

A tibble called clinic has a column age. Which line pulls that column out as a vector so you can run mean() on it?

Pre-test

You want the average HbA1c across the loaded clinic dataset. Which line does this?

Pre-test

What makes a Quarto (.qmd) report reproducible?

Pre-test

Your data is in an Excel worksheet rather than a .csv. Which function reads it into a tibble?

Pre-confidence

I can read a .csv into R with read_csv() from readr, after switching the package on with library(), and store the result in a named object.

Not at all confident
Fully confident
Pre-confidence

I can confirm a dataset loaded correctly using the tibble printout, glimpse(), and head(), and pull out a single column as a vector with $.

Not at all confident
Fully confident
Pre-confidence

I can explain what a Quarto (.qmd) report is and why rendering it from the raw data and code makes an analysis reproducible.

Not at all confident
Fully confident
Section 2 of 7

2 Introduction

In Part I you built values and vectors by hand with <- and c(). Real clinical data does not arrive one value at a time — it lives in a file. This part takes you from a file on disk to a loaded, checked dataset, and then to a report that computes every number in it from that raw data instead of copying it by hand.

This part of the module covers three foundations you will reach for in every analysis that follows:

  • Reading data — loading a .csv file into R with read_csv() and giving the result a name you can work with.
  • Checking the load — using the tibble printout, glimpse(), and head() to confirm every column came in as the type you expected.
  • Reproducible reports — writing a Quarto document that mixes your prose with runnable code, so one render rebuilds the whole report from the data.

By the end of this part you will be able to read a clinical dataset into R with read_csv(), confirm it loaded correctly with glimpse() and head(), pull a single column out as a vector with $, and explain why a Quarto report is reproducible when a copy-and-pasted number is not.

read_csv() reads a plain-text .csv file into R as a tibble, a tidy table whose printout names the data type of every column.
read_csv() reads a plain-text .csv file into R as a tibble, a tidy table whose printout names the data type of every column.

Try every snippet in the R Scratchpad on the right — the dataset diabetes_clinic.csv is already loaded and waiting for you.

Section 3 of 7

3 Reading clinical data into R

Clinical data usually lives in a .csv file — a plain-text table with one row per patient. You read one into R with read_csv() from the readr package.

A package is a bundle of extra functions; you switch it on for the session with library(). readr is part of the tidyverse, the family of packages this course uses throughout.

The file diabetes_clinic.csv is already in your workspace. Read it in and give it a name:

Try it out

Try this snippet in the R Scratchpad on the right.

Try this snippet
library(readr)
clinic <- read_csv("diabetes_clinic.csv")
clinic

What you get back is a tibble — the tidyverse's tidy version of a table. A tibble prints cleanly: it shows the first ten rows, and under each column name it tells you the column's type (dbl for a number, chr for text, and so on), so you can spot at a glance whether a column came in as you expected.

read_csv() reads a plain-text .csv file into R as a tibble, a tidy table whose printout names the data type of every column.
read_csv() reads a plain-text .csv file into R as a tibble, a tidy table whose printout names the data type of every column.

Two functions help you confirm the data loaded correctly. glimpse() lists every column down the page with its type and first few values; head() shows the first six rows across.

Try it out

Try this snippet in the R Scratchpad on the right.

Try this snippet
library(readr)
library(dplyr)
clinic <- read_csv("diabetes_clinic.csv")
glimpse(clinic)
head(clinic)
glimpse() and head() are two views of the same loaded data frame, one rotating it so every column and its type runs down the page, the other keeping the table shape to show the first six rows across.
glimpse() and head() are two views of the same loaded data frame, one rotating it so every column and its type runs down the page, the other keeping the table shape to show the first six rows across.

To pull one column out as a vector, write the dataset name, a dollar sign, and the column name: clinic$age is every patient's age.

Try it out

Try this snippet in the R Scratchpad on the right.

Try this snippet
library(readr)
clinic <- read_csv("diabetes_clinic.csv")
clinic$age
mean(clinic$age)
A column pulled out with $ is just an ordinary vector, identical to one built by hand with c(), so every vector function you already know (like mean()) works on it directly with nothing new to learn.
A column pulled out with $ is just an ordinary vector, identical to one built by hand with c(), so every vector function you already know (like mean()) works on it directly with nothing new to learn.

Excel files work almost the same way: read_excel("file.xlsx") from the readxl package reads a worksheet into a tibble. Reach for read_csv() when you can, though — a plain .csv is readable on any machine and never hides formatting surprises.

Section 4 of 7

4 Reproducible reports with Quarto

There is one more piece you will use: Quarto. A Quarto document (a .qmd file) mixes your written explanation with runnable R code in the same file. When you render it, R runs every code chunk fresh and drops the results — numbers, tables, plots — straight into a finished HTML, PDF, or Word report.

This matters because it makes your analysis reproducible: the report is generated from the raw data and the code in one click, so no number is ever copied by hand. You will write Quarto reports in RStudio for your labs; the in-browser scratchpad here is just for trying things out.

A Quarto report is generated by running the code in your .qmd, so every number and figure in the finished document is computed from the raw data rather than copied in by hand.
A Quarto report is generated by running the code in your .qmd, so every number and figure in the finished document is computed from the raw data rather than copied in by hand.
Section 5 of 7

5 Put it together

Before you start on a worked example, let’s try and familiarize yourself with the syntax first.

This Parsons problem gives you the right lines in the wrong order, plus a few lines that do not belong. Drag the correct lines into order and leave the wrong ones in the bank.

Parsons problem · Load and check a dataset

All the lines you need are in the Line bank on the left — some may be distractors you should leave behind. Drag the lines you need into the Your solution column on the right, in the correct order, then click Check.

Task: Switch on readr, read the clinic file into an object called clinic, then show its first rows.

Line bank
  • clinic <- read_csv("diabetes_clinic.csv")
  • library(readr)
  • library(read_csv)
  • head(clinic)
  • head[clinic]
  • clinic <- read.csv(diabetes_clinic.csv)
Your solution
  • Drop lines here, in order.

The worked example below fades the support as you go: first you study a full solution, then you fill the gaps, then you solve a fresh one on your own.

Worked example · From a column of results to a summary

Work through this example in three stages. You unlock each stage only after the tutor confirms the previous one. Each stage removes more of the scaffolding — by the end you are writing it yourself.

Problem: You have eight fasting glucose readings (mmol/L). Store them in one object, then report how many there are and their average.

Stage 1 · Study the solved example
Fully solved solution
glucose <- c(5.4, 7.1, 6.8, 9.2, 5.9, 8.3, 6.1, 7.7)
length(glucose)
mean(glucose)
Walk-through
  1. Combine the eight readings into one vector with c()
  2. length() counts how many values the vector holds
  3. mean() adds them up and divides by the count
c() bundles many separate values into one named vector, which summary functions like length() and mean() then read as a single object to return a count and an average.
c() bundles many separate values into one named vector, which summary functions like length() and mean() then read as a single object to return a count and an average.
Section 6 of 7

6 Check your understanding

You have reached the end of the module. Try the same questions again — your answers here, paired with your pre-test answers, are how we measure what the module taught you. Answer every question and confidence rating, then click Submit and see results to view your score.

Post-test

The file diabetes_clinic.csv is in your workspace. Which line reads it into R and stores it in an object called clinic?

Post-test

read_csv() lives in the readr package. Which line switches that package on for the session before you use it?

Post-test

read_csv() returns a tibble. What does the tibble printout tell you under each column name that helps you check the load?

Post-test

You have just run clinic <- read_csv("diabetes_clinic.csv"). Which line lists every column down the page with its type and first values, to confirm it loaded correctly?

Post-test

A tibble called clinic has a column age. Which line pulls that column out as a vector so you can run mean() on it?

Post-test

You want the average HbA1c across the loaded clinic dataset. Which line does this?

Post-test

What makes a Quarto (.qmd) report reproducible?

Post-test

Your data is in an Excel worksheet rather than a .csv. Which function reads it into a tibble?

Post-confidence

I can read a .csv into R with read_csv() from readr, after switching the package on with library(), and store the result in a named object.

Not at all confident
Fully confident
Post-confidence

I can confirm a dataset loaded correctly using the tibble printout, glimpse(), and head(), and pull out a single column as a vector with $.

Not at all confident
Fully confident
Post-confidence

I can explain what a Quarto (.qmd) report is and why rendering it from the raw data and code makes an analysis reproducible.

Not at all confident
Fully confident
Section 7 of 7

7 Your results

Here is how your post-test answers compare with your pre-test answers. The pre/post pairing is the most reliable way to see what this module actually taught you.

Your score

Submit the post-test to see your results.

Muddiest point

What is the one thing from this module that is still unclear to you?

Rate this module

Overall, how would you rate this module?

How likely are you to recommend this module to a peer? (0 = not at all, 10 = extremely likely)