
Data reading and processing
Source:vignettes/articles/Data-reading-and-processing.Rmd
Data-reading-and-processing.RmdThis vignette provides a summary of the straightforward and common use of REDCapDM to interact with REDCap data.
Read REDCap data into R
REDCapDM allows you to import REDCap data either from
local files or via an API connection. Both methods return a structured
list containing your dataset, dictionary, and event-form mapping (for
longitudinal projects).
1. Using local files
Ensure you have exported the following files from REDCap (only the R script and its data CSV must be in the same directory; the dictionary and the event-form mapping can be in other folders):
R data file – reads and processes your REDCap dataset, obtained by choosing R Statistical Software (
.ror.R). Please, keep the R script and its data CSV in the same directory: the script reads the CSV from its folder.Dictionary file – CSV file containing variable metadata
Event-form mapping file (only for longitudinal projects) – CSV describing which forms belong to each event
Then, provide the paths to these files using the
redcap_data() function:
dataset <- redcap_data(
data_path = "C:/Users/username/example.r",
dic_path = "C:/Users/username/example_dictionary.csv",
event_path = "C:/Users/username/events.csv" # optional for longitudinal projects
)⚠️ Tip: If your project is not longitudinal, you can omit
event_path.
2. Using API connection
You can also import data directly from REDCap using your project’s API token and URL:
dataset_api <- redcap_data(
uri = "https://redcap.com/api/",
token = "YOUR_API_TOKEN_HERE"
)This method automatically retrieves the event-form mapping for longitudinal projects.
With the API, redcap_data() makes several requests: the
metadata of the project (dictionary, events, instruments, …) and the
records, whose values and labels are read separately (in batches of
records, and the labels again if the records changed). The record
identifiers are kept as text while they are read, so long identifiers
(e.g. 9007199254740993, beyond the precision of the numbers
of R) or identifiers with zeros on the left (e.g. 007) are
not changed; as with the exported files, the identifier is a number only
when its text is that number. The labels of the events, repeated
instruments and Data Access Groups (the .factor columns)
are given to each row by its code, such as the unique event name, and
they are checked with the metadata of the project: if someone changes
the records while they are read, the labels are read again, and the
function stops with an error if they keep changing.
⚠️ Tip: Keep your API token secure and do not share it publicly.
3. Understanding the output
redcap_data()
returns a list with these elements (event_form only in
longitudinal projects, when it is available):
| Element | Description |
|---|---|
data |
Imported dataset containing all REDCap variables |
dictionary |
Metadata for each variable, including labels, types, and branching logic |
event_form |
Mapping between events and forms (only for longitudinal projects) |
Process and transform REDCap data
After importing your dataset into R, REDCapDM provides
several functions to process and transform your data
efficiently. The main function is rd_transform(),
which performs multiple operations in one step.
1. Core transformations
rd_transform() performs the following actions:
| Transformation | Description |
|---|---|
| Variable deletion | Remove specific variables or patterns |
| Recalculate fields | Recalculate the calculated fields in R and compare them with the
values of REDCap (the different ones are added as _recalc
columns; REDCap isn’t modified) |
| Checkbox transformation | Convert checkbox variables into separate variables with option names |
| Factor conversion | Replace original variables with their factor version |
| Branching logic conversion | Translate REDCap logic into R logic for easier processing |
2. Basic usage
The minimum required inputs are the dataset and dictionary. For longitudinal projects, include the event-form mapping for full functionality.
You can provide these inputs either as a single list
(output of redcap_data()) or as separate
arguments:
#Option A: Provide the full project list
covican_transformed <- rd_transform(covican)
#Option B: Provide individual arguments
covican_transformed <- rd_transform(
data = covican$data,
dic = covican$dictionary,
event_form = covican$event_form
)The output is a list containing:
data: Transformed dataset
dictionary: Updated variable dictionary
event_form: Event-form mapping (if applicable)
results: Summary of all transformations
You can view the transformation results:
#Print the results of the transformation
covican_transformed$results1. Removing selected variables
2. Deleting variables that contain some patterns
3. Recalculating calculated fields and saving them as '[field_name]_recalc'
| Total calculated fields | Non-transcribed fields | Fields with different recalculated values |
|:-----------------------:|:----------------------:|:-----------------------------------------:|
| 2 | 0 (0%) | 1 (50%) |
| field_name | Transcribed? | Is equal? | Different values |
|:-------------------:|:------------:|:---------:|:----------------:|
| age | Yes | FALSE | 1 |
| screening_fail_crit | Yes | TRUE | 0 |
4. Transforming checkboxes: changing their values to No/Yes and changing their names to the names of its options.
Table: Checkbox variables advisable to be reviewed
| Variables without any branching logic |
|:-------------------------------------:|
| type_underlying_disease |
5. Replacing original variables for their factor version
6. Converting every branching logic in the dictionary into R logic
3. Splitting by event
For longitudinal projects, you can split the transformed dataset by
event using the final_format = "by_event" argument:
dataset <- rd_transform(covican,
final_format = "by_event")Where the transformed dataset is a tibble object, containing data frames for each event in the REDCap project.
dataset$data
#> # A tibble: 2 × 2
#> events df
#> <chr> <list>
#> 1 baseline_visit_arm_1 <df [190 × 35]>
#> 2 follow_up_visit_da_arm_1 <df [152 × 9]>4. Splitting by form
Or, alternatively, it can be split by form using
final_format = "by_form" argument:
dataset <- rd_transform(covican,
final_format = "by_form")Where the tibble object is composed by data frames corresponding to each form in the REDCap project.
dataset$data
#> # A tibble: 7 × 3
#> form events df
#> <chr> <list> <list>
#> 1 inclusionexclusion_criteria <chr [1]> <df [190 × 10]>
#> 2 demographics <chr [1]> <df [190 × 9]>
#> 3 comorbidities <chr [1]> <df [190 × 10]>
#> 4 cancer <chr [1]> <df [190 × 16]>
#> 5 vital_signs <chr [2]> <df [342 × 7]>
#> 6 laboratory_findings <chr [2]> <df [342 × 7]>
#> 7 microbiological_studies <chr [1]> <df [190 × 6]>Quick troubleshooting checklist
- Did you export the variable dictionary from REDCap? Without it, label conversions and checkbox expansions are error-prone.
- Are API calls failing? Verify
uri,tokenand your project’s export permissions.
For more information, consult the complete vignettes available at: https://bruigtp.github.io/REDCapDM/articles/REDCapDM.html and https://bruigtp.github.io/REDCapDM/articles/data-processing.html