<- Sys.getenv("PROJ_pt_demo")
.token
<- "https://redcap.institution.edu/api/"
url
<- list(token = .token,
formData content = "report",
format = "csv",
report_id = "REPORT_ID", # Replace with report_id of interest
csvDelimiter = "",
rawOrLabel = "raw",
rawOrLabelHeaders = "raw",
exportCheckboxLabel = "false",
returnFormat = "csv")
<- httr::POST(url, body = formData, encode = "form")
response
<- httr::content(response) data
Retrieve REDCap Data in R
Version 1: Pull from reports created in REDCap
- Get API access in REDCap
- Generate a token in REDCap
- Create a system level environmental variable in Windows
- Search “Edit the system environment variables”
- Environment Variables …
- New under the system portion of the window
- Set the Variable name to something related to the project name e.g. “PROJ_pt_demo”
- Set the Variable value to the token
- Click OK and exit out of the windows
- In REDCap create a report of the events/fields/instruments of interest, and take note of the report id
- Use the following code to pull data, replacing the .token, url, and report_id values
- Modify rawOrLabel and rawOrLabelHeaders to download numeric (raw) values or character(label) values
- Could also download both in separate data frames to use a mixture
Version 2: Pull the entire data set
Create a credentials file
- Install the REDCapR pacakge
- Create a credentials files with the credential_create() command
install.packages("REDCapR")
::credential_create() REDCapR
- Fill out the file with the corresponding information including the project id and API token.
redcap_uri,username,project_id,token,comment"https://redcap.[institution].edu/api/","[user_name]","[project_id]","[token]","[optional comment to name the project]"
Save the file in a location that will not be tracked and potentially uploaded to GitHub by Git.
The following commands will pull all data from a REDCap project
<- REDCapR::retrieve_credential_local(
credential path_credential = path_to_credential,
project_id = the_project_id_of_interest
)
<- REDCapR::redcap_read(
data redcap_uri = credential$redcap_uri,
token = credential$token
$data )
Create a function using a project_id (pid) as an input
#' Retrieve REDCap data using user-specific credentials
#'
#' This function pulls data from a REDCap project using a locally stored
#' credentials file. It is designed to be used in environments where
#' REDCap credentials are stored in a fixed directory path with a user-specific
#' component (e.g., `C:/Users/<username>/path/to/redcap_credentials/credentials`).
#'
#' @param pid Integer. The REDCap project ID.
#'
#' @return A `data.frame` containing the REDCap project data retrieved using
#' the API token and URI stored in the credential file.
#'
#' @details This function requires the `REDCapR` package and assumes that the
#' local credentials file has been created using `REDCapR::credential_create()`
#' or similar method. The file path is dynamically generated using the system
#' username and a fixed relative path.
#'
#' @examples
#' \dontrun{
#' df <- get_redcap_data(pid = 12345)
#' head(df)
#' }
#'
#' @importFrom REDCapR retrieve_credential_local redcap_read
#' @export
<- function(pid) {
get_redcap_data # Set path to credentials file -----------------------------------------------
<- "C:/Users/"
prefix <- Sys.getenv("USERNAME")
user <- "/path/to/redcap_credentials/credentials"
docs
<- paste0(prefix, user, docs)
path_credential
# Retrieve credential object -------------------------------------------------
<- REDCapR::retrieve_credential_local(
credential path_credential = path_credential,
project_id = pid
)
# Read data ------------------------------------------------------------------
<- REDCapR::redcap_read(
data redcap_uri = credential$redcap_uri,
token = credential$token
$data
)
return(data)
}