Linkr
Home Resources Tools Documentation Blog Demo
FR
Learn programming
  • Why learn to code?
  • Programming fundamentals
  • Introduction to SQL
  • Introduction to R
  • Introduction to Python
4/5
8 min Boris Delange, Martin Castan, Lou Vignais · 02/07/2026

Introduction to R

What R is for, the language of statistics. The tidyverse, filtering and visualising data, Shiny, machine learning with tidymodels, Positron, and free resources to get started.

In a nutshell

R is a language built for statistics and data analysis. Once data has been extracted with SQL, R is used to clean it, compute indicators and produce publication-quality charts and tables. It is very common in biostatistics and clinical research. You generally use it in a dedicated working environment such as Positron or RStudio, with a set of libraries called the tidyverse.

This article assumes you have gone through Why learn to code? and the introduction to SQL. SQL pulls data out of the database; R then takes over to analyse it.

Why R in clinical research

R was created in the early 1990s by two statisticians at the University of Auckland, Ross Ihaka and Robert Gentleman (official R FAQ), for statistical computing and graphics. That explains its popularity in healthcare:

  • Statistical methods (tests, regressions, survival models…) are available natively or through proven libraries.
  • The charts it produces are of excellent quality.
  • It is often the language taught in medicine and public health, and therefore the one your future colleagues and supervisors use.

The working environment

To write and run R, you generally use a dedicated working environment, which gathers in a single window the code editor, the console that runs commands, the charts and the help.

  • Positron, developed by Posit (the company behind RStudio), is the one we recommend. Free, it handles R and Python and offers tools designed for data analysis: a pane listing the variables in memory, a data explorer to browse, sort and filter a table as in a spreadsheet, and a plot history.
  • RStudio, its predecessor, is still widely used and works the same way.
  • VS Code is a very versatile general-purpose editor: you can write R, Python, SQL and many other languages in it. For R, however, it requires installing and configuring an extension, and is less convenient for exploring your data.
Diagram of the Positron interface: the code editor and the console on the left, the Variables and Plots panes on the rightDiagram of the Positron interface: the list of database connections and the data explorer, showing a table with the distribution of each columnDiagram of the Positron interface: the AI assistant on the left, the Variables and Plots panes on the rightDiagram of the Positron interface: the publishing pane and the preview of an interactive application

You write code in the editor and run it in the console; on the right, the Variables pane lists the objects in memory and the Plots pane shows the figures produced.

You connect to a database, then the data explorer shows a table as in a spreadsheet, with the distribution of each column.

A built-in AI assistant helps you write and fix code, with access to the session's variables and plots.

Once the analysis is done, you can publish an interactive application or a document straight from Positron.

Illustrations: positron.posit.co

The tidyverse: working with data

The tidyverse is a set of libraries designed to work together, with the same logic and the same syntax. On its own, it covers the vast majority of what you need to work with health research data:

  • dplyr to manipulate data: filter, select, group, summarise (the topic of this section);
  • ggplot2 to create charts (next section);
  • tidyr to reshape a table, for example to go from long to wide format;
  • readr to import files, such as CSV files;
  • stringr, lubridate and forcats to work with text, dates and categorical variables;
  • tibble and purrr for more readable tables and to repeat the same operation.
The hexagonal logos of the nine core tidyverse libraries: dplyr, ggplot2, tidyr, readr, tibble, purrr, stringr, forcats and lubridate
The nine core tidyverse libraries. Logos: tidyverse.org.

A single line, library(tidyverse), loads them all.

What is a library?

A library (or package) is a set of ready-made functions, written by others, that you add to R to extend its capabilities. You install them once with install.packages(“name”), then load them in each script with library(name).

dplyr provides a small vocabulary of verbs that mirror the building blocks of a SQL query: filter() filters rows (like WHERE), select() chooses columns (like SELECT), group_by() + summarise() aggregate (like GROUP BY). You chain them with the pipe |>, read as “then”: take the data, then filter, then group…

R
library(dplyr)

# We start from a `patients` table with the columns sex, age and creatinine
patients |>
filter(sex == "F", age > 65) |>      # we keep women over 65
group_by(sex) |>                     # we group by sex
summarise(
  n = n(),                           # we count the patients
  mean_creat = mean(creatinine, na.rm = TRUE)  # and compute the mean creatinine
)
Result
sexnmean_creat
F3155.3

If you read the SQL article, you recognise the logic: it’s the same reasoning — filter, group, summarise — in a different syntax.

Before any analysis, you look at what the data is like. A few functions come up systematically:

head(patients)      # we display the first rows
summary(patients)   # we summarise each column: min, max, mean, missing values
nrow(patients)      # we count the rows

These three commands already give a good sense of a dataset’s structure and quality.

Visualising with ggplot2

ggplot2 builds a chart in layers: you declare the data, what goes on the x and y axes, then the type of plot.

R
library(ggplot2)

ggplot(patients, aes(x = age, y = creatinine)) +  # we create a chart with age on x and creatinine on y
geom_point(color = "#2563eb", size = 3) +       # we add a scatter plot
geom_smooth(method = "lm", color = "#2563eb",   # and a linear trend line,
            fill = "#2563eb", alpha = 0.15) +   # with its confidence interval
labs(                                           # we add the title and the axis names
  title = "Creatinine by age",
  x = "Age (years)",
  y = "Creatinine (µmol/L)"
) +
theme_minimal(base_size = 14) +                 # we pick a clean theme
theme(plot.title = element_text(hjust = 0.5, face = "bold"))  # and centre the title, in bold
Result
Scatter plot of creatinine by age, with a trend line and its confidence interval, produced by ggplot2 Scatter plot of creatinine by age, with a trend line and its confidence interval, produced by ggplot2

Each + adds a layer. That is what makes ggplot2 so easy to pick up: in just a few lines, you already have a clean figure, with the regression line and its confidence interval computed automatically. You can then customise it at will (colours, themes, annotations) and export it for a paper or a presentation. To see everything that is possible, browse the R Graph Gallery, which shows hundreds of charts with their code.

Shiny: interactive web applications

With the Shiny library, you build interactive web applications entirely in R. Users pick parameters from menus, and tables and charts update instantly.

A Shiny application that groups flowers by their measurements (k-means algorithm): changing a variable or the number of clusters updates the chart immediately. Example from Posit’s Shiny gallery.

A few dozen lines of code are enough for an application like this one. In healthcare, Shiny is used for example to build a dashboard to follow a cohort, to let clinicians explore results without writing code, or to share a calculation tool with a team. To get an idea of what is possible, browse the Shiny gallery.

Machine learning with tidymodels

R can also do machine learning. The tidymodels collection of libraries, designed in the same spirit as the tidyverse, offers a single syntax to train and evaluate very different models: logistic regression, random forest, gradient boosting…

The example below uses the Pima dataset, shipped with R: the clinical measurements (blood glucose, BMI, age…) of 532 Pima Indian women living in Arizona, each labelled with whether or not she has diabetes. We train a random forest on some of the patients, then measure how well it predicts diabetes in the others.

R
library(tidymodels)

# We load the Pima dataset, already split into training and test data
data(Pima.tr, Pima.te, package = "MASS")

set.seed(42)
model <- rand_forest(mode = "classification") |>  # we pick a random forest
fit(type ~ ., data = Pima.tr)                    # and train it to predict diabetes (type)

predict(model, Pima.te, type = "prob") |>          # we predict the probability of diabetes on the test data
bind_cols(Pima.te) |>                            # we join the true values
roc_auc(truth = type, .pred_Yes, event_level = "second")  # and compute the AUC
Result
.metric.estimator.estimate
roc_aucbinary0.828

The AUC measures how well the model tells diabetic patients apart from the others: 1 is a perfect prediction, 0.5 is chance.

R or Python for machine learning?

tidymodels covers the classic methods, but R’s machine learning ecosystem remains more limited than Python’s: fewer methods are available, and deep learning is done almost exclusively in Python. For an ambitious machine learning project, the scikit-learn library, presented in the introduction to Python, is the reference.

R and Linkr

The statistical analyses you design in the Study Designer — defining a population, computing variables — correspond to what you would do in R after extraction. Understanding R lets you extend an analysis beyond what a point-and-click tool offers, and verify your results.

You can also write and run R directly in Linkr, with its built-in IDE: the code editor, the console and the charts sit in the same window as your data. Try it in the demo.

Resources to learn

R for Data Science (2nd edition)
Type: Online book Language: English Cost: Free Duration: Several weeks Level: Beginner to intermediate Prerequisites: None (install R and Positron or RStudio)

What you'll learn

  • The complete workflow: import, tidy, transform, visualise, communicate.
  • Written by Hadley Wickham (creator of the tidyverse) and Mine Çetinkaya-Rundel, with exercises in each chapter.
  • The reference to learn the tidyverse in depth.
RStudio Education — Beginners
Type: Guided path Language: English Cost: Free Duration: ~10 h Level: Complete beginner Prerequisites: None

What you'll learn

  • The official path, from installing R and RStudio to your first charts.
  • Ideal to start before tackling R for Data Science.
Mastering Shiny
Type: Online book Language: English Cost: Free Duration: Several weeks Level: Intermediate Prerequisites: Basics of R and the tidyverse

What you'll learn

  • Build web applications with Shiny, from your first interface to complex applications.
  • Written by Hadley Wickham, the reference on Shiny.

Recommended path for R

  1. Install R and Positron, then follow RStudio Education — Beginners for your first steps (~10 h).
  2. Deepen your knowledge of the tidyverse with R for Data Science, the reference.
  3. To go further and build web applications, continue with Mastering Shiny.
  • R is the language of statistics: ideal for analysing and visualising data once extracted with SQL.
  • You generally work in Positron or RStudio, with the tidyverse — a set of libraries that covers most data manipulation needs.
  • The verbs filter(), select(), group_by(), summarise() mirror the building blocks of a SQL query; ggplot2 produces a clean figure in a few lines.
  • Shiny makes it simple to build interactive web applications, entirely in R.
  • tidymodels brings machine learning to R, with an ecosystem more limited than Python's.
  • Resources: R for Data Science, then Mastering Shiny.
  • You can also code in R directly in Linkr's IDE.

About the authors

Boris Delange
Boris Delange

Intensive-care physician · academic lecturer in medical informatics

View profile
Martin Castan
Martin Castan

Physician in anaesthesiology and critical care, trained in data science

View profile
Lou Vignais
Lou Vignais

Physician in pediatrics, trained in health data science

View profile
PreviousIntroduction to SQLNextIntroduction to Python

Product

  • Home
  • Demo

Resources

  • Documentation
  • Resources
  • Tools
  • Blog

Community

  • Framagit source code
  • Github source code

About

  • InterHop.org
  • Contact

2021–2026 InterHop — CC BY-NC-SA 4.0 (site) · AGPLv3 (software)