diff --git a/R/DESCRIPTION b/R/DESCRIPTION index ab0f3d3..dcae790 100644 --- a/R/DESCRIPTION +++ b/R/DESCRIPTION @@ -9,7 +9,9 @@ Description: Split REDCap repeating instruments output into multiple tables. This will take raw output from a REDCap export and split it into a base table and child tables for each repeating instrument. Depends: R (>= 3.4.0) -Suggests: RCurl, +Suggests: + RCurl, + httr, jsonlite, testthat License: GPL-3 @@ -19,5 +21,5 @@ RoxygenNote: 6.0.1 URL: https://github.com/SpectrumHealthResearch/REDCapRITS BugReports: https://github.com/SpectrumHealthResearch/REDCapRITS/issues Collate: - 'JSON2data.frame.r' + 'process_user_input.r' 'REDCap_split.r' diff --git a/R/R/JSON2data.frame.r b/R/R/JSON2data.frame.r deleted file mode 100644 index 7b97f16..0000000 --- a/R/R/JSON2data.frame.r +++ /dev/null @@ -1,37 +0,0 @@ -JSON2data.frame <- function (x) { - - if (inherits(x, "data.frame")) { - - return(x) - - } else if (inherits(x, "character")) { - - if (requireNamespace("jsonlite", quietly = TRUE)) { - - return(jsonlite::fromJSON(x)) - - } else { - - stop( - "The package 'jsonlite' is needed to convert ", - deparse(substitute(x)), - " into a data frame.", - "\n Either install 'jsonlite' or pass ", - deparse(substitute(x)), - " as a 'data.frame'.", - call. = FALSE - ) - - } - - } else { - - stop( - deparse(substitute(x)), - " must be a 'data.frame' or JSON string of class 'character'.", - call. = FALSE - ) - - } - -} diff --git a/R/R/REDCap_split.r b/R/R/REDCap_split.r index f1ac040..ea55290 100644 --- a/R/R/REDCap_split.r +++ b/R/R/REDCap_split.r @@ -4,11 +4,12 @@ #' and child tables for each repeating instrument. Metadata #' is used to determine which fields should be included in each resultant table. #' -#' @param records Exported project records. May be a \code{data.frame} or -#' \code{character} vector containing JSON from an API call. -#' @param metadata Project metadata (the data dictionary). May be a -#' \code{data.frame} or \code{character} vector containing JSON from an API +#' @param records Exported project records. May be a \code{data.frame}, +#' \code{response}, or \code{character} vector containing JSON from an API #' call. +#' @param metadata Project metadata (the data dictionary). May be a +#' \code{data.frame}, \code{response}, or \code{character} vector containing +#' JSON from an API call. #' @author Paul W. Egeler, M.S., GStat #' @examples #' \dontrun{ @@ -65,13 +66,13 @@ #' } #' @return A list of \code{"data.frame"}s: one base table and zero or more #' tables for each repeating instrument. -#' @include JSON2data.frame.r +#' @include process_user_input.r #' @export REDCap_split <- function(records, metadata) { # Process user input - records <- JSON2data.frame(records) - metadata <- JSON2data.frame(metadata) + records <- process_user_input(records) + metadata <- process_user_input(metadata) # Get the variable names in the dataset vars_in_data <- names(records) diff --git a/R/R/process_user_input.r b/R/R/process_user_input.r new file mode 100644 index 0000000..9da1601 --- /dev/null +++ b/R/R/process_user_input.r @@ -0,0 +1,53 @@ +process_user_input <- function (x) { + UseMethod("process_user_input", x) +} + +process_user_input.default <- function(x, ...) { + stop( + deparse(substitute(x)), + " must be a 'data.frame',", + " a 'response',", + " or a 'character' vector containing JSON.", + call. = FALSE + ) +} + +process_user_input.data.frame <- function(x, ...) { + x +} + +process_user_input.character <- function(x, ...) { + + if (!requireNamespace("jsonlite", quietly = TRUE)) { + stop( + "The package 'jsonlite' is needed to convert ", + deparse(substitute(x)), + " into a data frame.", + "\n Either install 'jsonlite' or pass ", + deparse(substitute(x)), + " as a 'data.frame'.", + call. = FALSE + ) + } + + jsonlite::fromJSON(x) + +} + +process_user_input.response <- function(x, ...) { + + if (!requireNamespace("httr", quietly = TRUE)) { + stop( + "The package 'httr' is needed to convert ", + deparse(substitute(x)), + " into a data frame.", + "\n Either install 'httr' or pass ", + deparse(substitute(x)), + " as a 'data.frame'.", + call. = FALSE + ) + } + + httr::content(x, as = "text") + +} diff --git a/R/man/REDCap_split.Rd b/R/man/REDCap_split.Rd index 7a1b827..8bd4793 100644 --- a/R/man/REDCap_split.Rd +++ b/R/man/REDCap_split.Rd @@ -7,12 +7,13 @@ REDCap_split(records, metadata) } \arguments{ -\item{records}{Exported project records. May be a \code{data.frame} or -\code{character} vector containing JSON from an API call.} +\item{records}{Exported project records. May be a \code{data.frame}, +\code{response}, or \code{character} vector containing JSON from an API +call.} \item{metadata}{Project metadata (the data dictionary). May be a -\code{data.frame} or \code{character} vector containing JSON from an API -call.} +\code{data.frame}, \code{response}, or \code{character} vector containing +JSON from an API call.} } \value{ A list of \code{"data.frame"}s: one base table and zero or more