mirror of
https://github.com/agdamsbo/REDCapCAST.git
synced 2024-11-22 05:20:23 +01:00
Turned routines to preprocess user inputs into S3 methods.
Still needs to be tested.
This commit is contained in:
parent
ef4819e1fc
commit
1053329a62
@ -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
|
This will take raw output from a REDCap export and split it into a base table
|
||||||
and child tables for each repeating instrument.
|
and child tables for each repeating instrument.
|
||||||
Depends: R (>= 3.4.0)
|
Depends: R (>= 3.4.0)
|
||||||
Suggests: RCurl,
|
Suggests:
|
||||||
|
RCurl,
|
||||||
|
httr,
|
||||||
jsonlite,
|
jsonlite,
|
||||||
testthat
|
testthat
|
||||||
License: GPL-3
|
License: GPL-3
|
||||||
@ -19,5 +21,5 @@ RoxygenNote: 6.0.1
|
|||||||
URL: https://github.com/SpectrumHealthResearch/REDCapRITS
|
URL: https://github.com/SpectrumHealthResearch/REDCapRITS
|
||||||
BugReports: https://github.com/SpectrumHealthResearch/REDCapRITS/issues
|
BugReports: https://github.com/SpectrumHealthResearch/REDCapRITS/issues
|
||||||
Collate:
|
Collate:
|
||||||
'JSON2data.frame.r'
|
'process_user_input.r'
|
||||||
'REDCap_split.r'
|
'REDCap_split.r'
|
||||||
|
@ -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
|
|
||||||
)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -4,11 +4,12 @@
|
|||||||
#' and child tables for each repeating instrument. Metadata
|
#' and child tables for each repeating instrument. Metadata
|
||||||
#' is used to determine which fields should be included in each resultant table.
|
#' 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
|
#' @param records Exported project records. May be a \code{data.frame},
|
||||||
#' \code{character} vector containing JSON from an API call.
|
#' \code{response}, or \code{character} vector containing JSON from an API
|
||||||
#' @param metadata Project metadata (the data dictionary). May be a
|
|
||||||
#' \code{data.frame} or \code{character} vector containing JSON from an API
|
|
||||||
#' call.
|
#' 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
|
#' @author Paul W. Egeler, M.S., GStat
|
||||||
#' @examples
|
#' @examples
|
||||||
#' \dontrun{
|
#' \dontrun{
|
||||||
@ -65,13 +66,13 @@
|
|||||||
#' }
|
#' }
|
||||||
#' @return A list of \code{"data.frame"}s: one base table and zero or more
|
#' @return A list of \code{"data.frame"}s: one base table and zero or more
|
||||||
#' tables for each repeating instrument.
|
#' tables for each repeating instrument.
|
||||||
#' @include JSON2data.frame.r
|
#' @include process_user_input.r
|
||||||
#' @export
|
#' @export
|
||||||
REDCap_split <- function(records, metadata) {
|
REDCap_split <- function(records, metadata) {
|
||||||
|
|
||||||
# Process user input
|
# Process user input
|
||||||
records <- JSON2data.frame(records)
|
records <- process_user_input(records)
|
||||||
metadata <- JSON2data.frame(metadata)
|
metadata <- process_user_input(metadata)
|
||||||
|
|
||||||
# Get the variable names in the dataset
|
# Get the variable names in the dataset
|
||||||
vars_in_data <- names(records)
|
vars_in_data <- names(records)
|
||||||
|
53
R/R/process_user_input.r
Normal file
53
R/R/process_user_input.r
Normal 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")
|
||||||
|
|
||||||
|
}
|
@ -7,12 +7,13 @@
|
|||||||
REDCap_split(records, metadata)
|
REDCap_split(records, metadata)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{records}{Exported project records. May be a \code{data.frame} or
|
\item{records}{Exported project records. May be a \code{data.frame},
|
||||||
\code{character} vector containing JSON from an API call.}
|
\code{response}, or \code{character} vector containing JSON from an API
|
||||||
|
call.}
|
||||||
|
|
||||||
\item{metadata}{Project metadata (the data dictionary). May be a
|
\item{metadata}{Project metadata (the data dictionary). May be a
|
||||||
\code{data.frame} or \code{character} vector containing JSON from an API
|
\code{data.frame}, \code{response}, or \code{character} vector containing
|
||||||
call.}
|
JSON from an API call.}
|
||||||
}
|
}
|
||||||
\value{
|
\value{
|
||||||
A list of \code{"data.frame"}s: one base table and zero or more
|
A list of \code{"data.frame"}s: one base table and zero or more
|
||||||
|
Loading…
Reference in New Issue
Block a user