redcaptargets integrates REDCap
databases with the targets
pipeline toolkit to streamline reproducible clinical research workflows.
It leverages the
redcapAPI
package to connect to REDCap and automatically generates dynamic targets
for both metadata and instrument-level data. By leveraging REDCap
logging, redcaptargets detects meaningful changes and re-pulls data only
when needed, minimizing compute time and ensuring local copies remain up
to date. Modular factory functions minimize boilerplate and adapt
seamlessly to evolving study designs.
You can install the development version of redcaptargets from GitHub
using:
# install.packages("remotes") # if not already installed
remotes::install_github("overdodactyl/redcaptargets")If you’re using renv, you can
install the package with:
renv::install("overdodactyl/redcaptargets")REDCap doesn’t support SQL-like queries, so users often resort to pulling all data across all instruments into their workspace for analysis. This leads to slow, repetitive code, especially in longitudinal studies where:
-
New participants enroll continuously
-
Forms and surveys evolve
-
Analyses need frequent refreshes
redcaptargets simplifies this by integrating REDCap with the targets
pipeline toolkit:
-
Automatically detects meaningful changes via REDCap logs
-
Dynamically creates targets for each instrument and metadata
-
Skips unnecessary pulls to save time and compute
-
Enables modular, reproducible workflows with built-in caching via gittargets
This example shows how to use tar_redcap()—the main function in
redcaptargets—to pull all REDCap instruments and metadata into a
targets pipeline.
library(targets)
library(redcaptargets)
# Establish REDCap Connection
redcap_con <- redcapAPI::redcapConnection(
url = Sys.getenv("REDCAP_URL"),
token = Sys.getenv("REDCAP_INTERNAL_82203")
)
# Source functions in R/ directory
tar_source()
# Targets pipeline
list(
tar_redcap(redcap, redcap_con),
tar_target(
n_enrolled,
command = nrow(redcap_demographics),
description = "Enrollment Count"
)
)targets::tar_make()
#> + redcap_visits dispatched
#> ✔ redcap_visits completed [503ms, 432 B]
#> + redcap_labs dispatched
#> ✔ redcap_labs completed [112ms, 477 B]
#> + redcap_meta_db dispatched
#> ✔ redcap_meta_db completed [89ms, 568 B]
#> + redcap_demographics dispatched
#> ✔ redcap_demographics completed [131ms, 623 B]
#> + redcap_record_id dispatched
#> ✔ redcap_record_id completed [0ms, 62 B]
#> + n_enrolled dispatched
#> ✔ n_enrolled completed [0ms, 48 B]
#> ✔ ended pipeline [1.1s, 6 completed, 0 skipped]Each instrument in the REDCap project will have its own target, along with one for metadata:
manifest <- targets::tar_manifest()
manifest[c("name", "description")]
#> # A tibble: 6 × 2
#> name description
#> <chr> <chr>
#> 1 redcap_visits REDCap Data: visits
#> 2 redcap_labs REDCap Data: labs
#> 3 redcap_meta_db REDCap Metadata
#> 4 redcap_demographics REDCap Data: demographics
#> 5 redcap_record_id REDCap Record ID
#> 6 n_enrolled Enrollment Count