codecov R-CMD-check-win-macos R-CMD-check-linux Slack

ctrdata for aggregating and analysing clinical trials

The package ctrdata provides functions for retrieving (downloading) information on clinical trials from public registers, and for aggregating and analysing this information. Use with R for the European Union Clinical Trials Register (“EUCTR”, and (“CTGOV”, The motivation is to understand trends in design and conduct of trials, their availability for patients and their detailled results. The package is to be used within the R system.

Last reviewed on 2020-10-17 for version 1.4

Main features:

Remember to respect the registers’ terms and conditions (see ctrOpenSearchPagesInBrowser(copyright = TRUE)). Please cite this package in any publication as follows: Ralf Herold (2020). ctrdata: Retrieve and Analyze Clinical Trials in Public Registers. R package version 1.4,

Package ctrdata has been used for:


1. Install package in R

Package ctrdata is on CRAN and on GitHub. Within R, use the following commands to install package ctrdata:

# Install CRAN version:

# Alternatively, install development version: 

These commands also install the package dependencies, which are nodbi, jsonlite, httr, curl, clipr, xml2, rvest.

2. Command line tools perl, sed, cat and php (5.2 or higher)

These command line tools are required for ctrLoadQueryIntoDb(), the main function of package ctrdata.

In Linux and macOS (including version 10.15 Catalina), these are usually already installed.

For MS Windows, install cygwin: In R, run ctrdata::installCygwinWindowsDoInstall() for an automated minimal installation into c:\cygwin. Alternatively, install manually cygwin with packages perl, php-jsonc and php-simplexml into c:\cygwin. The installation needs about 160 MB disk space; no administrator credentials needed.


Once installed, a comprehensive testing can be executed as follows (this will take several minutes):

tinytest::test_package("ctrdata", at_home = TRUE)

Overview of functions in ctrdata

The functions are listed in the approximate order of use.

Function name Function purpose
ctrOpenSearchPagesInBrowser() Open search pages of registers or execute search in web browser
ctrFindActiveSubstanceSynonyms() Find synonyms and alternative names for an active substance
ctrGetQueryUrlFromBrowser() Import from clipboard the URL of a search in one of the registers
ctrLoadQueryIntoDb() Retrieve (download) or update, and annotate, information on trials from a register and store in database
dbQueryHistory() Show the history of queries that were downloaded into the database
dbFindIdsUniqueTrials() Get the identifiers of de-duplicated trials in the database
dbFindFields() Find names of variables (fields) in the database
dbGetFieldsIntoDf() Create a data.frame from trial records in the database with the specified fields
dfTrials2Long() 🆕 Transform a data.frame from dbGetFieldsIntoDf() into a long name-value data.frame, including deeply nested fields
dfName2Value() 🆕 From a long name-value data.frame, extract values for variables (fields) of interest (e.g., endpoints)
dfMergeTwoVariablesRelevel() Deprecated - Merge two simple variables into a new variable, optionally map values to a new set of values
installCygwinWindowsDoInstall() Convenience function to install a cygwin environment (MS Windows only)

Example workflow

The aim is to download protocol-related trial information and tabulate the trials’ status of conduct.


# Please review and respect register copyrights:
ctrOpenSearchPagesInBrowser(copyright = TRUE)
q <- ctrGetQueryUrlFromBrowser()
# * Found search query from EUCTR.

#                                                   query-term  query-register
# 1 query=cancer&age=under-18&phase=phase-one&status=completed           EUCTR

Under the hood, scripts and xml2json.php (in ctrdata/exec) transform EUCTR plain text files and CTGOV XML files to ndjson format, which is imported into the database. The database is specified first, using nodbi (using RSQlite or MongoDB as backend); then, trial information is retrieved and loaded into the database:

# Connect to (or newly create) an SQLite database 
# that is stored in a file on the local system:
db <- nodbi::src_sqlite(
  dbname = "some_database_name.sqlite_file", 
  collection = "some_collection_name")

# See section Databases below 
# for MongoDB as alternative

# Retrieve trials from public register:
  queryterm = q,
  con = db)

Tabulate the status of trials that are part of an agreed paediatric development program (paediatric investigation plan, PIP):

# Get all records that have values in the fields of interest:
result <- dbGetFieldsIntoDf(
  fields = c(
  con = db)

# Find unique trial identifiers for trials that have nore than 
# one record, for example for several EU Member States: 
uniqueids <- dbFindIdsUniqueTrials(con = db)

# Keep only unique / de-duplicated records:
result <- result[ result[["_id"]] %in% uniqueids, ]

# Tabulate the selected clinical trial information:
#                     a7_trial_is_part_of_a_paediatric_investigation_plan
# p_end_of_trial_status Information not present in EudraCT No Yes
#   Completed                                            6 25  14
#   Ongoing                                              5 60  19
#   Prematurely Ended                                    1  6   3
#   Restarted                                            0  1   0
#   Temporarily Halted                                   0  0   1
# Retrieve trials from another register:
  queryterm = "cond=neuroblastoma&rslt=With&recrs=e&age=0&intr=Drug", 
  register = "CTGOV",
  con = db)

Analyse some simple result details (see vignette for more examples):

# Get all records that have values in any of the specified fields
result <- dbGetFieldsIntoDf(
  fields = c(
  con = db)

# Transform all fields into long name - value format
result <- dfTrials2Long(df = result)
# View(result)

# [1.] get counts for arms into data frame
nsubj <- dfName2Value(
  df = result, 
  valuename = "clinical_results.baseline.analyzed_list.analyzed.count_list.count.value"
nsubj <- tapply(
  X = nsubj[["value"]], 
  INDEX = nsubj[["trial_id"]],
  FUN = sum,
  simplify = TRUE
nsubj <- data.frame(
  trial_id = names(nsubj),
  stringsAsFactors = FALSE,
  row.names = NULL

# [2.] count number of sites
nsite <- dfName2Value(
  df = result, 
  # some ctgov records use
  #, others use
  valuename = "^location.*name$"
nsite <- tapply(
  X = nsite[["value"]], 
  INDEX = nsite[["trial_id"]],
  FUN = length,
  simplify = TRUE
nsite <- data.frame(
  trial_id = names(nsite),
  stringsAsFactors = FALSE,
  row.names = NULL

# [3.] randomised?
ncon <- dfName2Value(
  df = result, 
  valuename = "study_design_info.allocation"
ncon <- data.frame(
  trial_id = ncon[["trial_id"]], 
  randomised = ![["value"]]) & ncon[["value"]] == "Randomized",
  stringsAsFactors = FALSE

# merge sets
nset <- merge(nsubj, nsite)
nset <- merge(nset, ncon)

# Example plot
ggplot(data = nset) + 
  labs(title = "Neuroblastoma trials with results",
       subtitle = "") +
    mapping = aes(
      x = nsite,
      y = nsubj,
      colour = randomised)) + 
  scale_x_log10() + 
ggsave(filename = "inst/image/README-ctrdata_results_neuroblastoma.png",
       width = 4, height = 3, units = "in")
Neuroblastoma trials


The database connection object con is created by calling nodbi::src_*(), with parameters that are specific to the database (e.g., url) and with a special parameter collection that is used by ctrdata to identify which table or collection in the database to use. Any such connection object can then be used by ctrdata and generic functions of nodbi in a consistent way, as shown in the table:

Purpose SQLite MongoDB
Create database connection dbc <- nodbi::src_sqlite(dbname = ":memory:", collection = "name_of_my_collection") dbc <- nodbi::src_mongo(db = "name_of_my_database", collection = "name_of_my_collection", url = "mongodb://localhost")
Use connection with ctrdata functions ctrdata::{ctr,db}*(con = dbc) ctrdata::{ctr,db}*(con = dbc)
Use connection with nodbi functions nodbi::docdb_*(src = dbc, key = dbc$collection) nodbi::docdb_*(src = dbc, key = dbc$collection)

Features in the works


Issues and notes

Trial records’ JSON in databases


Example JSON representation in MongoDB


Example JSON representation in SQLite