util_parse_redcap_rule: Interpret a 'REDcap'-style rule and create an expression,...

View source: R/util_parse_redcap_rule.R

util_parse_redcap_ruleR Documentation

Interpret a REDcap-style rule and create an expression, that represents this rule

Description

Interpret a REDcap-style rule and create an expression, that represents this rule

Usage

util_parse_redcap_rule(
  rule,
  debug = 0,
  entry_pred = "REDcapPred",
  must_eof = FALSE
)

Arguments

rule

character REDcap style rule

debug

integer debug level (0 = off, 1 = log, 2 = breakpoints)

entry_pred

character for debugging reasons: The production rule used entry point for the parser

must_eof

logical if TRUE, expect the input to be eof, when the parser succeeded, fail, if not.

Value

expression the interpreted rule

REDcap rules 1 REDcap rules 2 REDcap rules 3

For resolving left-recursive rules, StackOverflow helps understanding the grammar below, just in case, theoretical computer science is not right in your mind currently.

See Also

Other parser_functions: util_interpret_limits(), util_interpret_range(), util_parse_assignments(), util_parse_interval()

Examples

## Not run: 
#  rules:
# pregnancies <- 9999 ~ SEX == 'm' |  is.na(SEX)
# pregnancies <- 9998 ~ AGE < 12 |  is.na(AGE)
# pregnancies = 9999 ~ dist > 2 |  speed == 0

data.frame(target = "SEX_0",
  rule = '[speed] > 5 and [dist] > 42 or 1 = "2"',
  CODE = 99999, LABEL = "PREGNANCIES_NOT_ASSESSED FOR MALES",
  class = "JUMP")
ModifyiedStudyData <- replace in SEX_0 where SEX_0 is empty, if rule fits
ModifyedMetaData <- add missing codes with labels and class here

subset(study_data, eval(pregnancies[[3]]))

rule <-
 paste0('[con_consentdt] <> "" and [sda_osd1dt] <> "" and',
 ' datediff([con_consentdt],[sda_osd1dt],"d",true) < 0')

x <- data.frame(con_consentdt = c(as.POSIXct("2020-01-01"),
                as.POSIXct("2020-10-20")),
                sda_osd1dt = c(as.POSIXct("2020-01-20"),
                as.POSIXct("2020-10-01")))
eval(util_parse_redcap_rule(paste0(
  '[con_consentdt] <> "" and [sda_osd1dt] <> "" and ',
  'datediff([con_consentdt],[sda_osd1dt],"d", "Y-M-D",true) < 10')),
  x, util_get_redcap_rule_env())

util_parse_redcap_rule("[a] = 12 or [b] = 13")
cars[eval(util_parse_redcap_rule(
  rule = '[speed] > 5 and [dist] > 42 or 1 = "2"'), cars,
  util_get_redcap_rule_env()), ]
cars[eval(util_parse_redcap_rule(
  rule = '[speed] > 5 and [dist] > 42 or 2 = "2"'), cars,
  util_get_redcap_rule_env()), ]
cars[eval(util_parse_redcap_rule(
  rule = '[speed] > 5 or [dist] > 42 and 1 = "2"'), cars,
  util_get_redcap_rule_env()), ]
cars[eval(util_parse_redcap_rule(
  rule = '[speed] > 5 or [dist] > 42 and 2 = "2"'), cars,
  util_get_redcap_rule_env()), ]
util_parse_redcap_rule(rule = '(1 = "2" or true) and (false)')
eval(util_parse_redcap_rule(rule =
  '[dist] > sum(1, +(2, [dist] + 5), [speed]) + 3 + [dist]'),
cars, util_get_redcap_rule_env())

## End(Not run)


dataquieR documentation built on May 29, 2024, 7:18 a.m.