Skip to content
AMC-CPTPublic

About

Pharmacokinetics with R (약동학 with R) — companion repository: R code, frozen output, figures, and a vancomycin TDM Shiny app

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

17 Commits

Folders and files

Repository files navigation

Pharmacokinetics with R: code repository

한국어 설명은 README.ko.md 에 있습니다.

The R code of the textbook 『약동학 with R』 (Pharmacokinetics with R) by Kyun-Seop Bae.

The code printed in the book is the file in this repository, and the console output and the figures printed in the book were produced by running it. So the code in the book cannot drift away from the code that actually ran. Changing a value and running it again is the way this book asks to be studied.

The book is written in Korean, and so are the comments in nearly all of the code files. Everything else is not language-bound: the file names, the chapter numbering, the console output and the figures are the same in any language, and the TDM-Vanco web app below has an English interface. An English edition of the book is in preparation, and its code is already here: En/ holds the same snippets with the comments, labels and messages in English, together with their own frozen output and figures (see below).

What is here

Folder Contents
R/snippets/ 201 snippets, one file per name. The NN of chNN-name.R is the chapter number. The order they appear in the book is the order in R/build.R, not alphabetical
output/ the console output of each snippet, 200 files. ch11-ctl is a NONMEM control stream shown as an example and is not run
figures/ the 82 figures the snippets make
R/build.R remakes all of it. The session boundaries between chapters are in its comments
R/_freeze.R the tool that runs one snippet and freezes its output and figure
En/ the same for the English edition: translated snippets, their output and figures, and En/build.R (below)
ACRE/ the syntax-highlighting definitions (Syntax/) of the ACRE editor that §1.11 introduces, and its README
TDM-Vanco/ the vancomycin TDM Shiny app of §13.7 (below)

Running it

Rscript R/build.R     # the Korean edition's output/ and figures/
Rscript En/build.R    # the English edition's En/output/ and En/figures/

Run from the repository root, R/build.R remakes output/ and figures/ from scratch. It takes 12–15 minutes, most of it the FOCE-I estimation of Chapter 11 and the randomization test and bootstrap of Chapter 12. To look at one snippet, open that file and just run it.

The packages needed are these. A star marks one the author wrote.

Package Chapters What it does
NonCompart* 5, 9, 10 non-compartmental analysis (NCA)
wnl* 4, 7, 8, 16, 17 nonlinear regression and error models
nmw* 11, 13 NONMEM's estimation steps, reproduced in R
BE* 14 bioequivalence analysis
sasLM* 14 linear models in the form of SAS PROC GLM
LBI* 3 likelihood intervals
mathr* 2 numerical tools (machine epsilon, quadrature, numerical differentiation)
deSolve 2, 5, 7, 8, 10, 13, 17 numerical solution of differential equations
PowerTOST 14 power and sample size for equivalence trials
nlme 5, 14 example data and mixed-effects models
install.packages(c("NonCompart", "wnl", "nmw", "BE", "sasLM", "LBI", "mathr",
                   "deSolve", "PowerTOST", "nlme"))

Things to know

Some chapters share a session. Most snippets run in a fresh R session per chapter, but at three places a chapter inherits the objects of the one before it. That is deliberate: it lets the book look at the same data two ways, and saves printing the same function twice.

  • Ch 5 → Ch 6: Cpo
  • Ch 7 → Ch 8 → Ch 9: dat2, C2iv, th, D, CL, tobs (Chapter 8 runs inside Chapter 7's session)
  • Ch 11 → Ch 12: DATA, PRED, TH, OM, SG, EBE, r.fo, r.foce, cov.fo

Start a new session in between and the later chapter breaks. The boundaries are marked in the comments of R/build.R.

The random seeds are fixed. Every snippet that simulates carries its own set.seed(), so a rerun gives the numbers printed in the book.

En/: the English edition

En/R/snippets/chNN-*.R   the same code with the comments, labels and messages in English
En/build.R               runs them and makes En/output/ and En/figures/
En/output/               the English edition's frozen console output
En/figures/              the English edition's frozen figures (Latin face)

En/build.R sources R/build.R and R/_freeze.R and only redirects the three paths and the figure font, so the two editions share one pipeline: the same order of snippets, the same shared sessions and the same seeds. Outside the comments and the printed strings the code is identical, token for token, and every number in En/output/ is identical to its counterpart in output/; what differs is the labels. The English build takes as long as the Korean one.

Why the output and the figures are committed. Normally one records the code and not what it produced. An exception is made here so that someone who has not installed R can still see the results while reading the code. The code that remakes those files is in the same repository, so nothing is taken on trust.

TDM-Vanco: a vancomycin TDM web app

TDM-Vanco/ is a Shiny app that runs the TDM engine described in §13.7 of the book. Its point is that swapping the population model's output (THETA, OMEGA, SIGMA) is all it takes to move it to another drug.

Its only external dependency is shiny (commonmark is used to render the markdown of the help tab, and without it the help shows as plain text). Everything else is base R.

TDM-Vanco screen

The screen with synthetic data (TDM-Vanco/tests/fixtures/synthetic_patient.csv) loaded. Two observed concentrations give a MAP estimate of the individual parameters, and the next dose is proposed with a prediction interval.

Three ways to run it

1. Without downloading anything (if you have R)

shiny::runGitHub("PKwR", "AMC-CPT", subdir = "TDM-Vanco")

One line. It fetches from GitHub, unpacks into a temporary folder and starts.

2. From a clone

shiny::runApp("TDM-Vanco")     # from the repository root

3. In a browser, with no R at all

Open https://amc-cpt.github.io/PKwR/. No installation and no server.

GitHub cannot run R, and GitHub Pages serves static files only. But shinylive exports the app to WebAssembly, so the R runtime runs inside the browser and static hosting is enough. Both shiny and commonmark, which this app uses, are in the webR repository, so it works unchanged.

install.packages("shinylive")
shinylive::export("TDM-Vanco", "docs")   # builds the static site into docs/

The exported site is 66 MB, nearly all of it the R runtime. Putting that on main would add the same weight to the default-branch tarball that runGitHub() downloads in method 1, so it lives on a separate gh-pages branch here (Pages source: gh-pages / (root)). The runtime is fetched on the first visit and cached by the browser afterward.

The two differ in one way that matters: in the browser version R runs inside your own browser, so patient data you load is never transmitted anywhere. The cost is a slow first load.

If a server is wanted instead, this repository can be connected to shinyapps.io or Posit Connect Cloud.

Verification

tests/regression.R runs a regression check on the synthetic data in tests/fixtures/ and compares it against golden_baseline.rds. No real patient data is in this repository.

source("TDM-Vanco/tests/regression.R")

The book

『약동학 with R』 (Pharmacokinetics with R), Kyun-Seop Bae (University of Ulsan College of Medicine · Asan Medical Center). Three parts, seventeen chapters. The book is published separately.

For the solutions to the exercises, or to report an error, write to ksbae@acr.kr.

License

The R code in this repository and what it produces are under GPL-3 (LICENSE). The text of the book and its figure captions are not included here and are separately reserved.

The other books in the series

1 Scientific Computation with R https://github.com/AMC-CPT/SciCompR
2 Scientific Inference in Clinical Trials with R https://github.com/AMC-CPT/CTDA
4 Pharmacometrics with NONMEM and R https://github.com/AMC-CPT/PMx
5 Essentials of Clinical Drug Development (online appendix) https://github.com/AMC-CPT/CDD

About

Pharmacokinetics with R (약동학 with R) — companion repository: R code, frozen output, figures, and a vancomycin TDM Shiny app

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages