Package 'r4clinstats'

Title: Learn R for Clinical Statistics, Interactively
Description: A swirl-style, learn-by-doing course that teaches clinicians and clinical researchers to wrangle, tidy, visualise and analyse clinical data in R. Lessons are interactive 'learnr' tutorials graded by 'gradethis', built on the tidyverse and real-feeling synthetic clinical datasets.
Authors: A. P. S. Munro [aut, cre]
Maintainer: A. P. S. Munro <[email protected]>
License: MIT + file LICENSE
Version: 0.3.3
Built: 2026-09-22 17:39:06 UTC
Source: https://github.com/apsmunro/r4clinstats

Help Index


Check your R setup for r4clinstats

Description

Diagnoses the things that commonly stop a learner getting started on a managed Windows machine, and optionally fixes them: an out-of-date R, a package library living inside a synced OneDrive folder, no route to the package repositories, and missing packages. It prints a short report.

Usage

check_setup(install = interactive())

Arguments

install

Logical. Offer to install any missing packages. Defaults to interactive(); set FALSE to report only.

Value

Invisibly TRUE if everything needed is present, otherwise FALSE.

Examples

## Not run: 
check_setup()
check_setup(install = TRUE)

## End(Not run)

Synthetic longitudinal labs

Description

Repeated measurements for the patients cohort: systolic blood pressure and weight at three visits. Long format, one row per patient * visit * measure. Built reproducibly (set.seed(2025)) from patients; the generator is in data-raw/labs.R. SBP falls faster on the active arm, so a trend is there to be found once the data is pivoted.

Usage

labs

Format

A data frame with 600 rows and 5 variables:

id

Patient identifier, matching patients.

visit

Visit number: 0, 1 or 2.

weeks

Weeks since baseline: 0, 12 or 24.

measure

"sbp" or "weight".

value

Measured value.

Source

Synthetic.


Browse and launch the r4clinstats lessons

Description

Lists the course modules and opens one as an interactive tutorial in your browser. With no argument it lists the modules and returns you to the prompt; pass a module id (for example "m3" or 3) to open that one.

Usage

learn(module = NULL)

Arguments

module

Optional module id, such as "m3". If NULL, the modules are listed.

Details

There is deliberately no numbered pick-list. utils::menu() numbers from 1, so its numbers could never match module ids that start at m0, and a learner typing 10 for "m10" would open m9. One way in, learn("m10"), avoids both that and the separate ⁠Selection:⁠ mode, which rejects R code.

Value

Invisibly NULL. Called for the side effect of launching a tutorial.

Examples

## Not run: 
learn()        # list the modules
learn("m3")    # open Wrangling I

## End(Not run)

Synthetic messy line list

Description

A small, deliberately untidy dataset for the cleaning lesson (M6): the same problems you meet in real intake data. Synthetic and built reproducibly (set.seed(2027)); the generator is in data-raw/linelist.R. Everything except weight is stored as text, and the mess is intentional.

Usage

linelist

Format

A data frame with 50 rows and 6 variables:

id

Patient identifier, e.g. "L01".

sex

The same variable typed inconsistently ("Female", "female", "MALE", ...).

age

Age as text, including a few non-numeric entries that become NA.

smoker

Smoking status in lower case: "never", "former" or "current".

arm

Treatment arm: "Active" or "Placebo".

weight

Weight in kg, with some values missing (NA).

Source

Synthetic.


Synthetic trial outcomes

Description

One year of follow-up for the patients cohort, for the analysis lessons (M10-M13): treatment response, adverse events, and a time-to-event pair censored at one year. Built reproducibly (set.seed(2026)) from patients; the generator is in data-raw/outcomes.R. The signal is planted: response is likelier on the active arm, and the active arm's event hazard is lower. Like labs, the table carries no arm or age column on purpose, so the lessons join back to patients.

Usage

outcomes

Format

A data frame with 100 rows and 5 variables:

id

Patient identifier, matching patients.

response

Treatment response: "Y" or "N".

adverse_event

Any adverse event: "Y" or "N".

time

Days of event-free follow-up (1-365).

status

1 if the event occurred at time, 0 if censored at one year.

Source

Synthetic.


Synthetic clinical-trial patients

Description

A made-up cohort of 100 trial patients, one row each. Built reproducibly (set.seed(2024)) and containing no real patient data. The generator is in data-raw/patients.R. Columns are stored as character (not factor) on purpose, so the early lessons read naturally and M2 can teach factors.

Usage

patients

Format

A data frame with 100 rows and 7 variables:

id

Patient identifier, e.g. "P001".

age

Age in years (40-88).

sex

"Female" or "Male".

arm

Treatment arm: "Placebo" or "Active".

smoker

Smoking status: "Never", "Former" or "Current".

bmi

Body-mass index.

sbp

Systolic blood pressure (mmHg).

Source

Synthetic.


Start your capstone trial report

Description

Copies the course's Quarto report template into your working directory and opens it, ready to complete and render. This is the final exercise of the course (Lesson 14): a real, reproducible analysis report built outside the tutorial sandbox. The template renders as-is; you fill in one analysis chunk at a time.

Usage

start_report(path = "trial-report.qmd", completed = FALSE, overwrite = FALSE)

Arguments

path

Where to put the report. Defaults to trial-report.qmd in the current working directory.

completed

Logical. FALSE (the default) copies the template with TODO chunks for you to complete; TRUE copies the finished report, for comparing against when stuck.

overwrite

Logical. Overwrite path if it already exists. Defaults to FALSE so you cannot lose work by accident.

Value

Invisibly, the path to the new file.

Examples

## Not run: 
start_report()                  # the template, ready to complete
start_report(completed = TRUE)  # the finished version, to compare

## End(Not run)

Move your R package library out of a synced folder

Description

Creates a local package library and points R at it by writing R_LIBS_USER to your ⁠~/.Renviron⁠. Use this when check_setup() reports that your library sits inside OneDrive. Restart R afterwards for it to take effect.

Usage

use_local_library(
  path = file.path(Sys.getenv("LOCALAPPDATA", unset = path.expand("~")), "R", "library")
)

Arguments

path

Directory for the local library. Defaults to a folder under ⁠%LOCALAPPDATA%⁠ (or your home directory if that is unset).

Value

Invisibly, the library path.