| rd_rlogic | R Documentation |
Converts a REDCap logic expression into R-compatible logic. Processes one logic expression (logic) for one target variable (var) at a time. Supports common REDCap operators (and, or, =, <, >, etc.) and handles event-specific logic in longitudinal projects and repeated instruments. Logic involving smart variables may require manual review.
rd_rlogic(
project = NULL,
data = NULL,
dic = NULL,
event_form = NULL,
logic,
var
)
project |
A list containing the REDCap data, dictionary, and event mapping (expected |
data |
A |
dic |
A |
event_form |
Only applicable for longitudinal projects (presence of events). Event-to-form mapping for longitudinal projects. |
logic |
A single REDCap logic string (e.g., |
var |
A single string specifying the target variable the logic applies to. When |
Translates REDCap operators and functions into R equivalents:
= → ==, <> → !=, and → &, or → |.
Converts the functions if(), datediff(), round(), rounddown(), roundup(), sum(), mean(), median(), min(), max(), mod(), exponential(), isnumber(), isblankormissingcode(), contains(), not_contain(), starts_with(), ends_with(), left(), right(), mid(), find(), replace_text(), concat(), concat_ws(), upper(), lower(), trim(), length(), stdev(), isinteger(), year(), month() and day() to R equivalents.
As in REDCap, sum(), mean(), median(), min() and max() are computed for each row and ignore blank values.
Handles date transformations and empty strings ('') → NA.
Adjusts logic for longitudinal data using event_form if provided.
Evaluates the translated R logic against the dataset and returns the results.
With repeated instruments and repeating events, the logic is evaluated in the rows where var is collected. As in REDCap, in a repeating instrument the variables of the other forms take the value of the base row of the same record and event. A variable of another repeating instrument or repeating event can be used with its instance ([event][field][current-instance], the instance of the same number). Without it, the logic can't be translated when the instance to use isn't known: [field] of another instrument that repeats in the events of var, or [event][field] when its form repeats in that event or the event repeats. [event][field] of a form collected once in that event is valid, although the form repeats in other events.
As in REDCap, datediff() returns the absolute difference unless its last argument is true, its units are "y" (years of 365.2425 days), "M" (months of 30.44 days), "d", "h", "m" (minutes) and "s", "today" is the current date and "now" the current date and time. With two time fields (e.g. HH:MM), it is the difference between the two times of the same day, and its date format (e.g. 'hhmm') is not used.
The same variable can be used in several events (e.g. [visit_2_arm_1][weight] - [visit_1_arm_1][weight]).
Logic with smart variables or other REDCap functions may require manual inspection.
A list with:
The translated R expression. It can reference auxiliary columns or internal helpers used during translation; it is not necessarily reusable against the original dataset. Use eval or eval_data for the evaluated result.
The evaluated values in input row order, with NA outside the target events and instrument rows. A constant expression can return a single value for a non-longitudinal dataset without repeating instruments; use eval_data for one value per input row.
A data.frame with the record identifier, the event and repetition columns present in the dataset (redcap_event_name, redcap_repeat_instrument, redcap_repeat_instance) and eval, to identify the record of each evaluated value.
Other process functions:
rd_checkbox(),
rd_dates(),
rd_delete_vars(),
rd_dictionary(),
rd_factor(),
rd_insert_na(),
rd_recalculate(),
rd_split(),
rd_transform()
# Translate a single REDCap logic expression for one variable
covican |>
rd_rlogic(
logic = "if([exc_1]='1' or [inc_1]='0' or [inc_2]='0' or [inc_3]='0', 1, 0)",
var = "screening_fail_crit"
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.