This article outlines solutions to common errors, installation issues, and data behaviors encountered when working with the Castor EDC R Package (castoRedc).
1. Authentication Issues
Error: Bad Request (HTTP 400). Failed to get an access token
Error in oauth2.0_access_token(endpoint, app, code = code, user_params = user_params, ...): Bad Request (HTTP 400). Failed to get an access token
This error indicates that the Castor API rejected the authentication request.
How to fix:
-
Verify your credentials: Go to your Castor EDC Account Settings > Castor EDC API tab to confirm your Client ID and Client Secret.
- If you recently clicked Regenerate secret, any older secret was immediately invalidated. You must update your script with the new secret.
- Make sure no leading or trailing whitespace was accidentally included when copying and pasting your credentials.
-
Check your regional server (
base_url): By default, many scripts default to the EU server. If your study is hosted on another server, authenticating against the EU endpoint will fail or return no studies. Match yourbase_urlto the server where your study is hosted:-
EU (Standard):
"https://data.castoredc.com" -
US:
"https://us.castoredc.com" -
UK:
"https://uk.castoredc.com" -
Australia:
"https://au.castoredc.com"
-
EU (Standard):
library(castoRedc)
# Example connecting to the US server:
castor_api <- CastorData$new(
key = Sys.getenv("CASTOR_KEY"),
secret = Sys.getenv("CASTOR_SECRET"),
base_url = "https://us.castoredc.com"
)
2. Installation Errors
Error: installation of package '...' had non-zero exit status
Error: Failed to install 'castoRedc' from GitHub: (converted from warning) installation of package 'later' had non-zero exit status
This typically occurs when installing castoRedc via remotes or devtools and one of the underlying dependencies (such as later, httpuv, or curl) fails to compile from source.
How to fix:
Try the following methods in order:
-
Method 1: Install the failing package directly from CRAN as a binary
Installing the pre-compiled binary from CRAN bypasses the need for local compilation:install.packages("later") # Replace 'later' with whichever package failed remotes::install_github("castoredc/castoRedc") -
Method 2: Prevent compilation warnings from halting the install
By default,remotesconverts compiler warnings into fatal errors. You can disable this behavior:Sys.setenv(R_REMOTES_NO_ERRORS_FROM_WARNINGS = "true") remotes::install_github("castoredc/castoRedc") -
Method 3: Install all dependencies explicitly
Force R to resolve and install all required package dependencies:remotes::install_github("castoredc/castoRedc", dependencies = TRUE)
Error: HTTP error 403. API rate limit exceeded
Error: Failed to install 'castoRedc' from GitHub: HTTP error 403. API rate limit exceeded for <IP address>
GitHub imposes a rate limit of 60 unauthenticated requests per hour per IP address.
This often happens on institutional, university, or hospital networks where multiple users share an external IP address.
How to fix:
- Wait for the 60-minute rate limit window to reset.
-
Alternatively, supply a GitHub Personal Access Token (PAT):
remotes::install_github("castoredc/castoRedc", auth_token = "YOUR_GITHUB_PAT")
3. Package Dependencies and Updates
R packages are maintained and updated independently. Out-of-date dependencies can create version conflicts with castoRedc.
Keep your environment updated, especially the core packages castoRedc relies on: dplyr, tidyr, purrr, glue, jsonlite, and httr.
To update your dependencies, run:
install.packages(c("dplyr", "tidyr", "purrr", "glue", "jsonlite", "httr", "vctrs"))
4. Vector Recycling Issues
Error: Can't recycle '..1' (size ...) to match '..2' (size 0)
Error: Can't recycle `..1` (size 971) to match `..2` (size 0).
(Note: the actual size numbers will vary depending on your dataset.)
This error occurs during data restructuring (unnesting lists or combining tables) when tidyr or vctrs encounters mismatched or empty (length-0) records.
How to fix:
Update both tidyr and vctrs to their latest CRAN versions:
install.packages(c("tidyr", "vctrs"))
After updating, restart your R session (Ctrl+Shift+F10 on Windows / Cmd+Shift+0 on Mac in RStudio) and rerun your script.
5. Working with Blanks ("") vs. Missing Values (NA)
When inspecting exported data in R, you may observe that some fields contain NA while others appear completely blank (""). This is expected behavior and reflects Castor's underlying data audit state:
| Value in R | Meaning in Castor EDC |
|---|---|
NA |
Never entered: The field was never filled in for this participant. |
"" (Empty string)
|
Cleared: A value was previously entered and later deleted/cleared by a user. |
How to normalize empty strings to NA for statistical analysis:
If your downstream analysis requires all missing or empty values to be treated uniformly as NA, you can recode them:
-
Using Base R:
# Replace all empty strings across the entire data frame with NA studydata[studydata == ""] <- NA
-
Using
dplyr:library(dplyr) # Recode empty strings to NA for all character columns studydata <- studydata %>% mutate(across(where(is.character), ~ na_if(., "")))