Learn R Programming

transplantr

The transplantr package provides a set of vectorised functions for audit and clinical research in solid organ transplantation. These are particularly intended to work well with multiple datapoints in large series of data, where manual calculations would be particularly tedious.

The functions provided fall into three groups:

  • Donor and recipient risk indices
  • HLA mismatch level calculators
  • Estimated GFR calculators
  • Biochemical unit converters

Although the package was built with unit tests, inaccuracies cannot be completely excluded. it is not a medical device and should not be used for making clinical decisions.

Installation

transplantr can be installed from CRAN:

# install transplantr
install.packages("transplantr")

# load transplantr once installed
library(transplantr)

Development version

The development version can be installed from GitHub, if you want all the latest features, together with all the latest bugs and errors. Installing from CRAN is the best option for most users as the submitted packages have to pass some very pedantic automated tests before they can be hosted on CRAN. If you do want the caveat emptor, you have been warned version, this is how:

# install from GitHub
devtools::install_packages("johnasher/transplantr")

Tips on using transplantr

As vectorised functions, the functions can be applied across a whole dataset fairly rapidly. I find that the easiest way to do this is using a “pipe” of functions from the dplyr package. dplyr can be installed on its own or, as I would recommend, by installing the whole tidyverse family of packages - a family which includes the legendary ggplot2 graphing package.

# install whole tidyverse
install.packages("tidyverse")

# install just dplyr
install.packages("dplyr")

Although recommended, dplyr is not necessary for most transplantr functions to work. dplyr is needed for the EPTS and KDPI functions, and additionally stringr is needed for the hla_mm_level_str() function and also for the chi2dob() function, one unlikely to be needed by anyone working outside Scotland!

Biochemical units

By default, all the functions work with the units most commonly used in the UK, which for creatinine and bilirubin is µmol/l, but each function using either of these can be used with mg/dl instead by changing an optional units parameter to "US" or by calling a wrapper function suffixed with _US(); e.g. when calculating eGFR, the ckd_epi_US() function calls ckd_epi() using creatinine in mg/dl.

Albumin is generally reported in g/l in the UK, but more commonly as g/dl in the US. The few functions using albumin default to g/l but change to g/dl if the units parameter is set to "US" or the _US() wrapper function is called.

Which is the best option to use? Calling the wrapper function uses fewer keystrokes so is quicker to type, but as it is a function calling another function, there is a slight increase in computational overhead.

Using transplantr functions with dplyr

Let’s say you want to calculate MELD scores for a series of liver transplant candidates. OK, you probably actually want MELD-Na, but let’s go with MELD as it has fewer variables! The data is in a dataframe or tibble called “oltx.assessments” and the relevant variables are Patient.INR, Patient.Bilirubin, Patient.Creatinine and Patient.Dialysed. To add a new Patient.MELD variable to the dataframe, you would use a dplyr pipe with the mutate() verb:

oltx.assessments <- oltx.assessments %>%
  mutate(Patient.MELD = meld(INR = Patient.INR, bili = Patient.Bilirubin,
        creat = Patient.Creatinine, dialysis = Patient.Dialysed, units = "SI"))

The units = "SI" can be left out provided that creatinine and bilirubin are both in µmol/l. To switch to mg/dl, use units = "US" or call meld_US() instead.

Using transplantr functions with base R

Although I think dplyr makes life much easier when organising data, I concede that some people prefer to use base R functions instead. Using a vectorised function with multiple vector inputs is not easy in base R but can be done with the mapply(), or more easily with the pmap_dbl() from the purrr package.

# attach oltx.assessments to save a lot of typing!
attach(oltx.assessments)

# method using pmap_dbl()
oltx.assessments$Patient.MELD = pmap_dbl(list(Patient.INR, Patient.Bilirubin,
                                              Patient.Creatinine, Patient.Dialysed),
                                          meld, units = "SI")

# alternative method using mapply()
oltx.assessments$Patient.MELD = mapply(FUN = meld, Patient.INR, Patient.Bilirubin,
                                        Patient.Creatinine, Patient.Dialysed,
                                        MoreArgs = list(units = "SI"),
                                        SIMPLIFY = TRUE)

# detach oltx.assessments to avoid namespace errors
detach(oltx.assessments)

The advantage of using a dplyr pipe, apart from easier code, is speed. Benchmarking on a basic Linux laptop showed that the median time to perform vectorised calculation of 100,000 MELD scores was 115 milliseconds, compared with 6007 milliseconds using `pmap_dbl() and 6484 with mapply().

Using the functions with a single case

Although vectorised functions for multiple calculations are one of the best features of R, you might just want to collect data on a single case. This is very straightforward:

# using µmol/l
meld(INR = 2.1, bili = 34, creat = 201, dialysis = 0)

# using mg/dl
meld(INR =  2.1, bili = 2.0, creat = 2.3, dialysis = 0, units = "US")

# using mg/dl with wrapper function
meld_US(INR =  2.1, bili = 2.0, creat = 2.3, dialysis = 0)

More information

For more information, function documentation and usage vignettes, visit transplantr.txtools.net.

Copy Link

Version

Install

install.packages('transplantr')

Monthly Downloads

209

Version

0.2.0

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

John Asher

Last Published

February 28th, 2020

Functions in transplantr (0.2.0)

cockcroft

Creatinine clearance by Cockcroft-Gault equation
chi2dob

Convert CHI number to date of birth
bun_to_urea

Convert BUN to urea
et_dri

Eurotransplant Donor Risk Index in Liver Transplantation (ET-DRI)
creatinine_to_SI

Creatinine unit converter (mg/dl -> <U+00B5>mol/l)
creatinine_to_US

Creatinine unit converter (<U+00B5>mol/l -> mg/dl)
cockcroft_US

Creatinine clearance by Cockcroft-Gault equation (US units)
hla_mm_level

HLA mismatch level
bilirubin_to_US

Bilirubin unit converter (<U+00B5>mol/l -> mg/dl)
meld_na_US

MELD-Na score (US units)
meld_na

MELD-Na score
kidney.donors

Simulated dataset of donors to illustrate KDRI vignette.
kdpi

US KDPI
kdpi_US

US KDPI (US units)
kdpi_lookup

US KDPI lookup function
apri

AST to Platelet Ratio (APRI)
bar_score

BAR (Balance of Risk) score in liver transplantation
nankivell_spk_US

eGFR using the Nankivell-SPK formula (US units)
meld

MELD score
ckd_epi

eGFR by CKD-EPI equation
mismatches

Simulated dataset to illustrate mismatches for HLA vignette.
meld_US

MELD score (US units)
p_soft

P-SOFT Score
schwartz

eGFR by bedside Schwartz formula
p_soft_US

P-SOFT Score (US units)
ckd_epi_US

eGFR by CKD-EPI equation (US units)
epts

Estimated Post-Transplant Survival Score (EPTS)
epts_lookup

EPTS lookup function
ukkrri

UK Kidney Recipient Risk Index (NHSBT, 2019 version)
results_US

Simulated dataset to illustrate eGFR calculator vignette.
nankivell

eGFR using Nankivell formula
mdrd

eGFR by abbreviated MDRD equation
mdrd_US

eGFR by abbreviated MDRD equation (US units)
soft

SOFT score (Survival Outcomes Following Liver Transplantation)
nankivell_spk

eGFR using the Nankivell-SPK formula
schwartz_US

eGFR by bedside Schwartz formula (US units)
nankivell_US

eGFR using Nankivell formula (US units)
serial.results

Simulated dataset to illustrate serial results eGFR calculator vignette.
urea_to_bun

Convert urea to BUN
hla_mm_level_str

HLA mismatch level from string
uskdri

US Kidney Donor Risk Index
ukkrri_q

UK Kidney Recipient Risk Index Quartile (2019)
p_pass

P-PASS pre-procurement pancreas suitability score
soft2

SOFT score from P-SOFT
soft2_US

SOFT score from P-SOFT (US units)
peld

PELD score
watson_ukkdri

UK Kidney Donor Risk Index (2012 version)
walser_US

eGFR using the Walser formula (US units)
soft_US

SOFT score (Survival Outcomes Following Liver Transplantation) (US units)
walser

eGFR using the Walser formula
ukkdri

UK Kidney Donor Risk Index (NHSBT, 2019 version)
uskdri_US

US Kidney Donor Risk Index (US units)
ukkdri_q

UK Kidney Donor Risk Index Quartile (2019)
peld_US

PELD score (US units)
ibw

Ideal body weight
pedi_soft

Pedi-SOFT Score
liver_dri

Liver Donor Risk Index (DRI)
pdri

Pancreas donor risk index
liver.pts

Simulated dataset to illustrate MELD calculator vignette.
raw_epts

Estimated Post-Transplant Survival Score (EPTS)
ukeld_US

UKELD score (US units)
ukeld

UKELD score
results

Simulated dataset to illustrate eGFR calculator vignette.
bilirubin_to_SI

Bilirubin unit converter (mg/dl -> <U+00B5>mol/l)