R/parseBranchingLogic.R

Defines functions parseBranchingLogic

Documented in parseBranchingLogic

#' @name parseBranchingLogic
#' 
#' @title Parse Branching Logic
#' @description Branching logic from the REDCap Data data_dictionary is parsed into
#'   R Code and returned as expressions.  These can be evaluated if desired
#'   and allow the user to determine if missing values are truly missing or
#'   not required because the branching logic prevented the variable from being
#'   presented.
#'   
#' @param l A vector of REDCap branching logic statements.  These are usually
#'   passed as the vector \code{data_dict$branching_logic}.  
#'   
#' @details For a study, I was asked to identify which subjects had missing 
#'   values so that remaining data could be collected.  The initial pass of 
#'   \code{is.na} produced a lot of subjects missing values where there was no
#'   need to collect data because they did not qualify for some variables in 
#'   the branching logic.  Parsing the logic allowed me to determine which 
#'   values we expected to be missing and narrow the search to just those 
#'   subjects with legitimately missing values.
#'   
#' @return Returns a list of unevaluated expressions.
#' 
#' @author Benjamin Nutter


parseBranchingLogic <- function(l){
  l <- tolower(l)
  l <- gsub("\\n", " ", l)
  
  # Format operators
  l <- gsub(" or ", " | ", l)
  l <- gsub(" and ", " & ", l)
  l <- gsub("[=]", " == ", l)
  l <- gsub("[!] [=]", " !", l)
  l <- gsub("[<] [=]", " <", l)
  l <- gsub("[>] [=]", " >", l)
  l <- gsub("[<][>]", "!=", l)
  
  # Format checkbox names
  l <- gsub("([a-z,0-9,_])\\((?<=\\()(.*?)(?=\\))\\)",
                "\\1___\\2",
                l,
                perl = TRUE)
  
  # Need to format smart variables here
  
  # Remove brackets
  l <- gsub("([[]|[]])", "", l)
  
  # Create list of expressions
  lapply(l, function(x) ifelse(x=="", NA, parse(text=x)))
  
  # library(magrittr)
  # record_data %>% dplyr::group_by(!!id_field) %>% 
  #   mutate()
}
chillywings/rctools documentation built on Aug. 9, 2024, 11:52 p.m.