Prerequisites
DRE:
- Owner access to workspace, to install R / RStudio / Rtools in a virtual machine
- External access to CRAN in the workspace where you want to export the data to (open up port rule)
- External access to Castor in the workspace where you want to export the data to (open up port rule)
Castor:
- 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).
- R: https://cran.r-project.org/bin/windows/base/
- RStudio (for the user interface): https://rstudio.com/products/rstudio/download/#download
- 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:
- 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:
- install.packages("devtools")
- 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):
- devtools::install_local("Z:\\path_to_zip\\castoRedc-master.zip")
Now you can load the package:
- 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):
- Internet or network address: The name of the credential, between : symbol (we use :castor_client_id: in the example script)
- User name: Fill in a symbol, for example -
- 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):
- install.packages("keyring")
Use CastoR package
Example R code, with functions in CastoR package
- library(castoRedc)
- castor_api <- CastorData$new(key = keyring::key_get("castor_client_id"),
- secret = keyring::key_get("castor_secret"),
- base_url = "https://data.castoredc.com/"
- studies <- castor_api$getStudies()
- records <- castor_api$getRecords("study_id_as_string")
- study_data <- castor_api$getStudyData("study_id_as_string")
- report_data <- castor_api$getReportInstancesBulk("study_id_as_string")
- fields <- castor_api$getFields("study_id_as_string")
- steps <- castor_api$getSteps("study_id_as_string")
- phases <- castor_api$getPhases("study_id_as_string")
Example R code, not (yet) in CastoR package (e.g. option groups)
- library(httr)
- library(jsonlite)
- base_url = "https://data.castoredc.com/"
- castorOAuth = function(key, secret, baseurl) {
- castor_app = httr::oauth_app("CastorEDC", key = key, secret = secret)
- castor_endpoint = httr::oauth_endpoint(
- request = NULL,
- base_url = paste0(baseurl, "oauth"),
- access = "token",
- "authorize"
- )
- castor_token = httr::oauth2.0_token(
- castor_endpoint,
- castor_app,
- client_credentials = TRUE,
- use_oob = FALSE,
- cache = FALSE
- )
- castor_token
- }
- oauth_token <-
- castorOAuth(
- key = keyring::key_get("castor_client_id"),
- baseurl = base_url,
- secret = keyring::key_get("castor_secret")
- )
- headers <- add_headers(Authorization = paste('Bearer',
- oauth_token$credentials$access_token))
- request <-
- GET(base_url, path = "api/study/study_id_as_string/export/optiongroups",
- query = list(), headers)
- option_groups <- read.csv2(text = rawToChar(request$content))
For further information on using the package, please visit the
Castor website