Turned routines to preprocess user inputs into S3 methods.

Still needs to be tested.
This commit is contained in:
pegeler 2018-06-22 22:24:34 -04:00
parent ef4819e1fc
commit 1053329a62
5 changed files with 70 additions and 50 deletions

View File

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

View File

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

View File

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

53
R/R/process_user_input.r Normal file
View File

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

View File

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