Cleaning up R code and adding RStudio project.

This commit is contained in:
Egeler, Paul W 2018-05-25 12:02:21 -04:00
parent f25319366c
commit cbc39e288e
3 changed files with 47 additions and 28 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
*.html
.Rhistory
.Rproj.user

16
REDCapRITS.Rproj Normal file
View File

@ -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

View File

@ -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)
}