From cbc39e288e1c7c56a276c6f514ad808cf0185ce8 Mon Sep 17 00:00:00 2001 From: "Egeler, Paul W" Date: Fri, 25 May 2018 12:02:21 -0400 Subject: [PATCH] Cleaning up R code and adding RStudio project. --- .gitignore | 1 + REDCapRITS.Rproj | 16 +++++++++++++ REDCap_split.r | 58 +++++++++++++++++++++++++----------------------- 3 files changed, 47 insertions(+), 28 deletions(-) create mode 100644 REDCapRITS.Rproj diff --git a/.gitignore b/.gitignore index c812dd9..b2c6868 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.html .Rhistory +.Rproj.user diff --git a/REDCapRITS.Rproj b/REDCapRITS.Rproj new file mode 100644 index 0000000..e83436a --- /dev/null +++ b/REDCapRITS.Rproj @@ -0,0 +1,16 @@ +Version: 1.0 + +RestoreWorkspace: Default +SaveWorkspace: Default +AlwaysSaveHistory: Default + +EnableCodeIndexing: Yes +UseSpacesForTab: Yes +NumSpacesForTab: 2 +Encoding: UTF-8 + +RnwWeave: Sweave +LaTeX: pdfLaTeX + +AutoAppendNewline: Yes +StripTrailingWhitespace: Yes diff --git a/REDCap_split.r b/REDCap_split.r index a98fe21..6f35fb5 100644 --- a/REDCap_split.r +++ b/REDCap_split.r @@ -1,7 +1,6 @@ -# v0.0.0 #' Split REDCap repeating instruments table into multiple tables #' -#' This will take a raw data frame from REDCap and split it into a base table +#' This will take a raw \code{data.frame} from REDCap and split it into a base table #' and give individual tables for each repeating instrument. Metadata #' is used to determine which fields should be included in each resultant table. #' @@ -12,7 +11,7 @@ #' \dontrun{ #' library(jsonlite) #' library(RCurl) -#' +#' #' # Get the metadata #' result.meta <- postForm( #' api_url, @@ -20,7 +19,7 @@ #' content = 'metadata', #' format = 'json' #' ) -#' +#' #' # Get the records #' result.record <- postForm( #' uri = api_url, @@ -35,7 +34,7 @@ #' exportDataAccessGroups = 'false', #' returnFormat = 'json' #' ) -#' +#' #' records <- fromJSON(result.record) #' metadata <- fromJSON(result.meta) #' @@ -45,43 +44,46 @@ #' @export REDCap_split <- function(records, metadata) { - # Check to see if there were any repeating instruments + stopifnot(all(sapply(list(records,metadata), inherits, "data.frame"))) - if (!any(names(records) == "redcap_repeat_instrument")) { + # Check to see if there were any repeating instruments - warning("There are no repeating instruments.\n") + if (!any(names(records) == "redcap_repeat_instrument")) { - return(list(records)) + message("There are no repeating instruments in this data.") - } + return(list(records)) - # Clean the metadata - metadata <- metadata[metadata["field_type"] != "descriptive", 1:4] + } - # Identify the subtables in the data - subtables <- unique(records["redcap_repeat_instrument"]) - subtables <- subtables[subtables != ""] + # Clean the metadata + metadata <- + metadata[metadata$field_type != "descriptive", c("field_name", "form_name")] - # Split the table based on instrument - out <- split.data.frame(records, records["redcap_repeat_instrument"]) + # Identify the subtables in the data + subtables <- unique(records$redcap_repeat_instrument) + subtables <- subtables[subtables != ""] - # Delete the variables that are not relevant - for (i in names(out)) { + # Split the table based on instrument + out <- split.data.frame(records, records$redcap_repeat_instrument) - if (i == "") { + # Delete the variables that are not relevant + for (i in names(out)) { - out[[which(names(out) == "")]] <- - out[[which(names(out) == "")]][metadata[`!`(metadata[,2] %in% subtables), 1]] + if (i == "") { - } else { + out[[which(names(out) == "")]] <- + out[[which(names(out) == "")]][metadata[!metadata[,2] %in% subtables, 1]] - out[[i]] <- - out[[i]][c(names(records[1:3]),metadata[metadata[,2] == i, 1])] + } else { - } + out[[i]] <- + out[[i]][c(names(records[1:3]),metadata[metadata[,2] == i, 1])] - } + } - return(out) + } + + return(out) }