mirror of
https://github.com/agdamsbo/REDCapCAST.git
synced 2026-06-19 13:17:30 +02:00
Converted R code into a package
This commit is contained in:
parent
3bcc7f9482
commit
50e0496d8c
11 changed files with 154 additions and 19 deletions
2
R/.Rbuildignore
Normal file
2
R/.Rbuildignore
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
^.*\.Rproj$
|
||||
^\.Rproj\.user$
|
||||
3
R/.gitignore
vendored
Normal file
3
R/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
.Rproj.user
|
||||
.Rhistory
|
||||
.RData
|
||||
22
R/DESCRIPTION
Normal file
22
R/DESCRIPTION
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
Package: REDCapRITS
|
||||
Title: REDCap Repeating Instrument Table Splitter
|
||||
Version: 0.0.0
|
||||
Authors@R: person(
|
||||
"Paul",
|
||||
"Egeler",
|
||||
email = "paul.egeler@spectrumhealth.org",
|
||||
role = c("aut", "cre"))
|
||||
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,
|
||||
jsonlite,
|
||||
testthat
|
||||
License: GPL-3
|
||||
Encoding: UTF-8
|
||||
LazyData: true
|
||||
RoxygenNote: 6.0.1
|
||||
Collate:
|
||||
'JSON2data.frame.r'
|
||||
'REDCap_split.r'
|
||||
3
R/NAMESPACE
Normal file
3
R/NAMESPACE
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Generated by roxygen2: do not edit by hand
|
||||
|
||||
export(REDCap_split)
|
||||
37
R/R/JSON2data.frame.r
Normal file
37
R/R/JSON2data.frame.r
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
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
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
114
R/R/REDCap_split.r
Normal file
114
R/R/REDCap_split.r
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
#' Split REDCap repeating instruments table into multiple tables
|
||||
#'
|
||||
#' This will take output from a REDCap export and split it into a base table
|
||||
#' 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
|
||||
#' call.
|
||||
#' @author Paul W. Egeler, M.S., GStat
|
||||
#' @examples
|
||||
#' \dontrun{
|
||||
#' library(RCurl)
|
||||
#'
|
||||
#' # Get the metadata
|
||||
#' result.meta <- postForm(
|
||||
#' api_url,
|
||||
#' token = api_token,
|
||||
#' content = 'metadata',
|
||||
#' format = 'json'
|
||||
#' )
|
||||
#'
|
||||
#' # Get the records
|
||||
#' result.record <- postForm(
|
||||
#' uri = api_url,
|
||||
#' token = api_token,
|
||||
#' content = 'record',
|
||||
#' format = 'json',
|
||||
#' returnFormat = 'json'
|
||||
#' )
|
||||
#'
|
||||
#' # Convert exported JSON strings into a list of data.frames
|
||||
#' REDCap_split(records, metadata)
|
||||
#' }
|
||||
#' @return A list of \code{"data.frame"}s: one base table and zero or more
|
||||
#' tables for each repeating instrument.
|
||||
#' @include JSON2data.frame.r
|
||||
#' @export
|
||||
REDCap_split <- function(records, metadata) {
|
||||
|
||||
records <- JSON2data.frame(records)
|
||||
metadata <- JSON2data.frame(metadata)
|
||||
|
||||
# Check to see if there were any repeating instruments
|
||||
if (!any(names(records) == "redcap_repeat_instrument")) {
|
||||
|
||||
message("There are no repeating instruments in this data.")
|
||||
|
||||
return(list(records))
|
||||
|
||||
}
|
||||
|
||||
# Find the fields and associated form
|
||||
fields <- metadata[
|
||||
!metadata$field_type %in% c("descriptive", "checkbox"),
|
||||
c("field_name", "form_name")
|
||||
]
|
||||
|
||||
if (any(metadata$field_type == "checkbox")) {
|
||||
|
||||
checkbox_basenames <- metadata[
|
||||
metadata$field_type == "checkbox",
|
||||
c("field_name", "form_name")
|
||||
]
|
||||
|
||||
checkbox_fields <-
|
||||
do.call(
|
||||
"rbind",
|
||||
apply(
|
||||
checkbox_basenames,
|
||||
1,
|
||||
function(x)
|
||||
data.frame(
|
||||
field_name = names(records)[grepl(paste0("^", x[1], "___.+$"), names(records))],
|
||||
form_name = x[2],
|
||||
stringsAsFactors = FALSE,
|
||||
row.names = NULL
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
fields <- rbind(fields, checkbox_fields)
|
||||
|
||||
}
|
||||
|
||||
# Identify the subtables in the data
|
||||
subtables <- unique(records$redcap_repeat_instrument)
|
||||
subtables <- subtables[subtables != ""]
|
||||
|
||||
# Split the table based on instrument
|
||||
out <- split.data.frame(records, records$redcap_repeat_instrument)
|
||||
|
||||
# Delete the variables that are not relevant
|
||||
for (i in names(out)) {
|
||||
|
||||
if (i == "") {
|
||||
|
||||
out[[which(names(out) == "")]] <-
|
||||
out[[which(names(out) == "")]][fields[!fields[,2] %in% subtables, 1]]
|
||||
|
||||
} else {
|
||||
|
||||
out[[i]] <-
|
||||
out[[i]][c(names(records[1:3]),fields[fields[,2] == i, 1])]
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
out
|
||||
|
||||
}
|
||||
21
R/REDCapRITS.Rproj
Normal file
21
R/REDCapRITS.Rproj
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
Version: 1.0
|
||||
|
||||
RestoreWorkspace: No
|
||||
SaveWorkspace: No
|
||||
AlwaysSaveHistory: Default
|
||||
|
||||
EnableCodeIndexing: Yes
|
||||
UseSpacesForTab: Yes
|
||||
NumSpacesForTab: 2
|
||||
Encoding: UTF-8
|
||||
|
||||
RnwWeave: Sweave
|
||||
LaTeX: pdfLaTeX
|
||||
|
||||
AutoAppendNewline: Yes
|
||||
StripTrailingWhitespace: Yes
|
||||
|
||||
BuildType: Package
|
||||
PackageUseDevtools: Yes
|
||||
PackageInstallArgs: --no-multiarch --with-keep.source
|
||||
PackageRoxygenize: rd,collate,namespace
|
||||
53
R/man/REDCap_split.Rd
Normal file
53
R/man/REDCap_split.Rd
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
% Generated by roxygen2: do not edit by hand
|
||||
% Please edit documentation in R/REDCap_split.r
|
||||
\name{REDCap_split}
|
||||
\alias{REDCap_split}
|
||||
\title{Split REDCap repeating instruments table into multiple tables}
|
||||
\usage{
|
||||
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{metadata}{Project metadata (the data dictionary). May be a
|
||||
\code{data.frame} 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
|
||||
tables for each repeating instrument.
|
||||
}
|
||||
\description{
|
||||
This will take output from a REDCap export and split it into a base table
|
||||
and child tables for each repeating instrument. Metadata
|
||||
is used to determine which fields should be included in each resultant table.
|
||||
}
|
||||
\examples{
|
||||
\dontrun{
|
||||
library(RCurl)
|
||||
|
||||
# Get the metadata
|
||||
result.meta <- postForm(
|
||||
api_url,
|
||||
token = api_token,
|
||||
content = 'metadata',
|
||||
format = 'json'
|
||||
)
|
||||
|
||||
# Get the records
|
||||
result.record <- postForm(
|
||||
uri = api_url,
|
||||
token = api_token,
|
||||
content = 'record',
|
||||
format = 'json',
|
||||
returnFormat = 'json'
|
||||
)
|
||||
|
||||
# Convert exported JSON strings into a list of data.frames
|
||||
REDCap_split(records, metadata)
|
||||
}
|
||||
}
|
||||
\author{
|
||||
Paul W. Egeler, M.S., GStat
|
||||
}
|
||||
4
R/tests/testthat.R
Normal file
4
R/tests/testthat.R
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
# library(testthat)
|
||||
# library(REDCapRITS)
|
||||
#
|
||||
# test_check("REDCapRITS")
|
||||
Loading…
Add table
Add a link
Reference in a new issue