Translate REDCap branching logic into expressions evaluable in R. E.g.
| Translation step | Example expression |
| 0. Initial REDCap logic | [over_18]='1' and [signed_consent]<>'' |
| 1. Translate to R | over_18 == '1' & signed_consent != '' |
| 2. (Optional) Swap values/labels | over_18 == 'Yes' & signed_consent != '' |
| 3. (Optional) Use is.na | over_18 == 'Yes' & !is.na(signed_consent) |
| 4. (Optional) Use %in% | over_18 %in% 'Yes' & !is.na(signed_consent) |
Usage
translate_logic(
x,
use_value_labs = TRUE,
use_header_labs = FALSE,
use_is_na = TRUE,
use_in = TRUE,
drop_redundant = FALSE,
field_nchar_max = 80L,
meta_factors = NULL,
meta_dictionary = NULL,
on_error = "warn"
)Arguments
- x
A character vector of REDCap branching logic statements
- use_value_labs
Logical indicating whether to replace factor option values with labels (based on the mapping defined in
meta_factors). E.g.:y == '1'becomesy == 'Yes'- use_header_labs
Logical indicating whether to use labels instead of variable names as column names (based on the mapping defined in
meta_dictionary). E.g.:age >= 18becomes"Participant's age" >= 18- use_is_na
Logical indicating whether to replace REDCap-style tests for missingness with
is.na. E.g.:y == ""becomesis.na(y)y != ""becomes!is.na(y)- use_in
Logical indicating whether to replace instances of
==and!=associated with factor-type variables (as defined inmeta_factors) with%in%. E.g.:y == 'Yes'becomesy %in% 'Yes'y != 'Yes'becomes!y %in% 'Yes'- drop_redundant
Logical indicating whether to simplify expressions by removing redundant components from expressions that test both for equality and inequality with the same variable. E.g.:
var == "X" & var != ""becomesvar == "X"- field_nchar_max
Integer indicating the maximum number of characters to allow in field name labels before they are truncated and appended with "...". Defaults to 80L.
- meta_factors
A data frame containing variable names (column
field_name) and corresponding values (columnvalue) and labels (columnlabel) for factor-type variables. Fetch withmeta_factors. Only needed if eitheruse_value_labsoruse_inareTRUE.- meta_dictionary
A data frame containing variable names (column
field_name) and labels (columnfield_label). Fetch withmeta_dictionary. Only needed ifuse_header_labsisTRUE.- on_error
What to do if one or more elements of statements can't be translated into valid R expressions? Options are "ignore" (return
NAfor relevant elements), "warn" (returnNAfor relevant elements and give warning), or "fail" (throw error). Defaults to "warn".
Examples
# normally would fetch factor metadata with redcap::meta_factors(), but here
# we'll create a simple example by hand
df_factors <- data.frame(
field_name = c("head_household", "head_household"),
value = c("0", "1"),
label = c("No", "Yes"),
stringsAsFactors = FALSE
)
redcap_logic <- "head_household=1 and age<>\"\""
translate_logic(redcap_logic, meta_factors = df_factors)
#> [1] "head_household %in% \"Yes\" & !is.na(age)"