From 89995330985f327e0d8e05d0657c8770f782c872 Mon Sep 17 00:00:00 2001 From: "Egeler, Paul W" Date: Mon, 4 Jun 2018 16:45:44 -0400 Subject: [PATCH] Handles new fields added by REDCap data export R script --- R/R/REDCap_split.r | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/R/R/REDCap_split.r b/R/R/REDCap_split.r index efca58e..9f411fb 100644 --- a/R/R/REDCap_split.r +++ b/R/R/REDCap_split.r @@ -72,6 +72,7 @@ REDCap_split <- function(records, metadata) { c("field_name", "form_name") ] + # Process checkbox fields if (any(metadata$field_type == "checkbox")) { checkbox_basenames <- metadata[ @@ -85,13 +86,14 @@ REDCap_split <- function(records, metadata) { apply( checkbox_basenames, 1, - function(x) + function(x, y) data.frame( - field_name = names(records)[grepl(paste0("^", x[1], "___.+$"), names(records))], + field_name = y[grepl(paste0("^", x[1], "___[0-9]+$"), y)], form_name = x[2], stringsAsFactors = FALSE, row.names = NULL - ) + ), + y = names(records) ) ) @@ -99,6 +101,39 @@ REDCap_split <- function(records, metadata) { } + # Process form_complete fields + form_names <- unique(metadata$form_name) + form_complete_fields <- data.frame( + field_name = paste0(form_names, "_complete"), + form_name = form_names, + stringsAsFactors = FALSE + ) + + fields <- rbind(fields, form_complete_fields) + + # Process ".*\\.factor" fields supplied by REDCap's export data R script + factor_fields <- + do.call( + "rbind", + apply( + fields, + 1, + function(x, y) { + field_indices <- grepl(paste0("^", x[1], "\\.factor$"), y) + if (any(field_indices)) + data.frame( + field_name = y[field_indices], + form_name = x[2], + stringsAsFactors = FALSE, + row.names = NULL + ) + }, + y = names(records) + ) + ) + + fields <- rbind(fields, factor_fields) + # Identify the subtables in the data subtables <- unique(records$redcap_repeat_instrument) subtables <- subtables[subtables != ""]