Castor R-Package

Castor R-Package

Prerequisites

DRE:
  1. Owner access to workspace, to install R / RStudio / Rtools in a virtual machine
  2. External access to CRAN in the workspace where you want to export the data to (open up port rule)
  3. External access to Castor in the workspace where you want to export the data to (open up port rule)
Castor:
  1. View and Export rights in the study you want to export with R


Installing CastoRedc package

If you already have R, RStudio and RTools installed, you can skip steps 1 and 3. Note that the Install DRE Applications program contains older versions of R, RStudio and RTools. It is advised to follow the steps below to obtain the newest versions.
R Local Repositories (available in Install DRE Applications) contains the wrong version of devtools. Make sure to run the options line from step 4 to install packages directly from cran.r-project.org.
Step 1: Download R/RStudio/RTools installation files
To use the Castor R package, you need R, RStudio and Rtools installed on at least 1 VM in your workspace. Use the following links to download the installation files on your own computer, then transfer these files to your workspace via the Files tab in the DRE portal (https://mydre.org).
  1. R: https://cran.r-project.org/bin/windows/base/
  2. RStudio (for the user interface): https://rstudio.com/products/rstudio/download/#download
  3. Rtools (to install packages): https://cran.r-project.org/bin/windows/Rtools/ (choose the 64-bit version)
Step 2: Get CastoRedc package
Download the CastoRedc package to your own computer from the Castor github page by clicking on Code and then Download ZIP (see screenshot below). Transfer the .zip file to your workspace as well.


Step 3: Install R/RStudio/RTools
Log into the VM you want to use to connect to Castor using R. In the VM, find the R, RStudio and RTools installation files on the data (Z:/) drive and move them to the desktop. Next, run the installation files one by one as administrator (right-click -> run as administrator). Wait for each installation to finish before running the next one.

Step 4: Set package location
Put RTools on the PATH (see R project website for instructions). Run RStudio as administrator (Start -> RStudio [right-click] -> More -> run as administrator) and run the following line of code:
  1. options(download.file.method = "libcurl", repos = c("CRAN" = "https://cran.r-project.org/"))
N.B.: Each time you start R and want to install a package, you need to run this line of code.

Step 5: Install the devtools and generics package
Run the following lines of code or install these packages via Tools -> Install packages:
  1. install.packages("devtools")
  2. install.packages("generics")
N.B.: You may need to install the curl package (part of devtools) separately, if it receives a non-zero exit status during installation of devtools.

Step 6: Install CastoRedc package
Use the install_local function from the devtools package to install the castoRedc package from the downloaded .zip file (replace the path with the actual location of the .zip file):
  1. devtools::install_local("Z:\\path_to_zip\\castoRedc-master.zip")
Now you can load the package:
  1. library(castoRedc)

Authentication

Step 1: Retrieve Castor client ID and secret
On your own computer, follow the steps on the Castor site below to retrieve your client ID and secret (needed to connect to Castor and retrieve data):

Step 2: Set up Windows credentials
In your VM, go to the Start Menu and search for 'windows credentials'. Click on 'Manage Windows Credentials', then select 'Windows Credentials' and click on 'Add a generic credential'. Fill in the required information (also see screenshot below):
  1. Internet or network address: The name of the credential, between : symbol (we use :castor_client_id: in the example script)
  2. User name: Fill in a symbol, for example -
  3. Password: The client id you retrieved in step 1
  

Step 3: Install Keyring package
To safely store your client ID and secret, you can use the keyring R-package (see below for use): 
  1. install.packages("keyring")


Use CastoR package

Example R code, with functions in CastoR package

  1. library(castoRedc)

  2. castor_api <- CastorData$new(key = keyring::key_get("castor_client_id"),
  3.                              secret = keyring::key_get("castor_secret"),
  4.                              base_url = "https://data.castoredc.com/"

  5. studies <- castor_api$getStudies()
  6. records <- castor_api$getRecords("study_id_as_string")
  7. study_data <- castor_api$getStudyData("study_id_as_string")
  8. report_data <- castor_api$getReportInstancesBulk("study_id_as_string")
  9. fields <- castor_api$getFields("study_id_as_string")
  10. steps <- castor_api$getSteps("study_id_as_string")
  11. phases <- castor_api$getPhases("study_id_as_string")

Example R code, not (yet) in CastoR package (e.g. option groups)

  1. library(httr)
  2. library(jsonlite)

  3. base_url = "https://data.castoredc.com/"

  4. castorOAuth = function(key, secret, baseurl) {
  5.   castor_app = httr::oauth_app("CastorEDC", key = key, secret = secret)

  6.   castor_endpoint = httr::oauth_endpoint(
  7.     request = NULL,
  8.     base_url = paste0(baseurl, "oauth"),
  9.     access = "token",
  10.     "authorize"
  11.   )
  12.   castor_token = httr::oauth2.0_token(
  13.     castor_endpoint,
  14.     castor_app,
  15.     client_credentials = TRUE,
  16.     use_oob = FALSE,
  17.     cache = FALSE
  18.   )
  19.   castor_token
  20. }

  21. oauth_token <-
  22.   castorOAuth(
  23.     key = keyring::key_get("castor_client_id"),
  24.     baseurl = base_url,
  25.     secret = keyring::key_get("castor_secret")
  26.   )

  27. headers <- add_headers(Authorization = paste('Bearer',
  28.                                              oauth_token$credentials$access_token))

  29. request <-
  30.   GET(base_url, path = "api/study/study_id_as_string/export/optiongroups",
  31.       query = list(), headers)

  32. option_groups <- read.csv2(text = rawToChar(request$content))


More information (Castor site)

For further information on using the package, please visit the Castor website


    • Related Articles

    • Castor

      Setting up for Direct Access Castor is a cloud-based clinical data platform, enabling researchers to easily capture and integrate data from clinicians, patients, devices, wearables, and EHR systems. The Castor data capture system is available through ...
    • R and RStudio

      Disclaimers: This article is written by somebody who does not work with R on the regular basis.  While R is frequently used on myDRE, please understand, anDREa is tool agnostic and formally does not provide support to tooling such as R While the ...
    • Using Castor in Python

      Introduction anDREa would like to thank Reinier van Linschoten for bringing this package and description to our attention. This is a Python package for interacting with the API of Castor Electronic Data Capture (EDC). The package contains functions ...
    • Installing R and Bioconductor packages

      Recently some users experienced issues with accessing R-packages and Bioconductor-packages. The article R and RStudio has been rewritten in order to easily install R and Bioconductor-packages in your Workspace.
    • FAQ

      In this article, we collect Frequently Asked Questions. Please create a ticket via this website or mail us at support@mydre.zohodesk.eu in case your question is not on this list, and you think it should be. The myDRE platform and anDREa What is ...