| redcap_data | R Documentation |
Import REDCap data into R either from REDCap's exported R file or directly via the REDCap API. The function returns a list with the dataset, the project dictionary (metadata) and, for longitudinal projects, the instrument–event mapping (event_form) when available.
redcap_data(
data_path = NA,
dic_path = NA,
event_path = NA,
uri = NA,
token = NA,
sep = ",",
filter_field = NULL,
survey_fields = FALSE,
rm_mdc = TRUE,
mdc = NULL
)
data_path |
Path to exported R file (use with |
dic_path |
Path to the dictionary file (CSV or XLSX; use with |
event_path |
Path to the event-form mapping file (CSV or XLSX) for longitudinal projects (downloadable via the |
uri |
REDCap API base URI (use with |
token |
REDCap API token (use with |
sep |
Character string specifying the field separator for the exported dictionary CSV file. Default |
filter_field |
Optional character vector of field names to request from the API. It must include the record
identifier (the first field of the project, e.g. |
survey_fields |
Logical; include survey-related fields when pulling via API. Default |
rm_mdc |
Logical. Remove Missing Data Codes (default |
mdc |
Optional Missing Data Codes of the project, besides the default ones of REDCap ( |
Two import modes are supported:
Exported files — use data_path (REDCap R export) and dic_path (dictionary CSV/XLSX).
API — use uri and token to pull data and metadata directly from REDCap.
If the project is longitudinal, provide event_path (instrument–event mapping)
or the function will attempt to fetch it from the API when using API mode.
With the API, the metadata of the project (dictionary, project information, events, instruments, ...) and the
records are read with several requests: the values and their labels are read separately (in batches of records,
and the labels again if the records changed). The record identifiers are read as text in
all of them (long identifiers such as 9007199254740993, or 100000000000000, are not changed), and the
identifier is a number only when its text is that number, as with the exported files. The labels of the events
(redcap_event_name.factor), repeated instruments and data access groups are given to each row by its code (the
unique event name, instrument or data access group), and they are checked with the metadata of the project (its
events, arms, instruments and data access groups): if the records changed between the values and the labels, the labels are
read again, and the function stops if they keep changing. When that metadata can't be read, those columns have
the codes instead of the labels, with a warning (except a single code with a single label in all the rows, e.g. the
data access group of a user assigned to a group).
Steps for using exported data in REDCap:
Use the REDCap Export Data function and choose R Statistical Software format.
REDCap generates:
A CSV file with observations.
An R script to format variables for import.
Keep the R script and its CSV file in the same directory (the script reads the CSV from its folder). The dictionary and the event mapping can be in other folders.
A list with:
data: Imported dataset.
dictionary: Variable dictionary (project meta data).
event_form: Event-form mapping for longitudinal projects (if applicable).
With the exported files, dic_path is required to load the project dictionary (with the API, the dictionary is
read from REDCap).
Use either exported-files mode (data_path + dic_path) or API mode (uri + token) — not both.
For exported files, REDCap's R export is required for data_path. Dictionary and event files must be CSV or XLSX.
## Not run:
# From exported files
out <- redcap_data(
data_path = "project_export.r",
dic_path = "project_dictionary.csv",
event_path = "instrument_event_map.csv"
)
# From API
out_api <- redcap_data(
uri = "https://redcap.example.org/api/",
token = "REPLACE_WITH_TOKEN"
)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.