Castor R-package


  • DRE: Workspace access (for installing R / RStudio / Rtools you need admin rights, workspace ownership, in the virtual machine)

  • Open up the port to Castor by a workspace owner, in the workspace where you want to export the data to (target)

  • Castor: View and Export rights in the study you want to export with R (source)

Install CastoR package

  1. To use the Castor R package, you need R, Rtools and the local CRAN repository. If you have not installed those yet, open the Install DRE applications and install:

  • R

  • R local repositories

  • RStudio (for the user interface)

  • Rtools (be sure to follow the step of creating a text file .Renviron with the PATH variable in it, see screenshot below)

3. Go to the Castor github page and download the package by clicking on Code and Download ZIP

4. Upload the downloaded zipped folder (default name: to your workspace

5. Install the devtools and generics package:



6. Use install_local function from the devtools package:


7. Now you can load the package:


  1. Follow the steps on the Castor site below, to retrieve your client ID and secret (needed to connect to Castor and retrieve data):

2. For safely storing your client ID and secret, we can use the keyring R-package:


3. Go to the Start Menu and search for 'windows credentials'. Click on 'Manage Windows Credentials':

4. Click on 'Windows Credentials'

5. Click on 'Add a generic credential'

6. Fill in the required information:

  • Internet or network address: The name of the credential, between : symbol (we use castor_client_id as name in the example script)

  • User name: Fill in a symbol, for example -

  • Password: The client id you retrieved in step 1

7. Do the same for the Castor secret:

  • Internet or network address: The name of the credential, between : symbol (we use castor_secret as name in the example script)

  • User name: Fill in a symbol, for example -

  • Password: The Castor secret you retrieved in step 1

Use CastoR package

Example R code, with functions in CastoR package


castor_api <- CastorData$new(key = keyring::key_get("castor_client_id"),

secret = keyring::key_get("castor_secret"),

base_url = "")

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)



base_url = ""

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",



castor_token = httr::oauth2.0_token(



client_credentials = TRUE,

use_oob = FALSE,

cache = FALSE




oauth_token <-


key = keyring::key_get("castor_client_id"),

baseurl = base_url,

secret = keyring::key_get("castor_secret")


headers <- add_headers(Authorization = paste('Bearer',


request <-

GET(base_url, path = "api/study/study_id_as_string/export/optiongroups",

query = list(), headers)

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

More information (Castor site)

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