rd_rlogic: Translate REDCap Logic to R Logic

View source: R/rd_rlogic.R

rd_rlogicR Documentation

Translate REDCap Logic to R Logic

Description

[Stable]

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.

Usage

rd_rlogic(
  project = NULL,
  data = NULL,
  dic = NULL,
  event_form = NULL,
  logic,
  var
)

Arguments

project

A list containing the REDCap data, dictionary, and event mapping (expected redcap_data() output). Overrides data, dic, and event_form.

data

A data.frame or tibble with the REDCap dataset.

dic

A data.frame with the REDCap dictionary.

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., "if([exc_1]='1' or [inc_1]='0', 1, 0)").

var

A single string specifying the target variable the logic applies to. When event_form is supplied, it must be a field of the dictionary; its form determines the target events.

Details

  • 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.

Value

A list with:

rlogic

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.

eval

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.

eval_data

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.

See Also

Other process functions: rd_checkbox(), rd_dates(), rd_delete_vars(), rd_dictionary(), rd_factor(), rd_insert_na(), rd_recalculate(), rd_split(), rd_transform()

Examples

# 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"
  )


REDCapDM documentation built on Nov. 27, 2026, 9:06 a.m.