mirror of
https://github.com/agdamsbo/REDCapCAST.git
synced 2024-11-22 13:30:23 +01:00
Converted R code into a package
This commit is contained in:
parent
3bcc7f9482
commit
50e0496d8c
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
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,15 +1,17 @@
|
|||||||
#' Split REDCap repeating instruments table into multiple tables
|
#' Split REDCap repeating instruments table into multiple tables
|
||||||
#'
|
#'
|
||||||
#' This will take a raw \code{data.frame} from REDCap and split it into a base table
|
#' This will take output from a REDCap export and split it into a base table
|
||||||
#' and give individual 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 \code{data.frame} containing project records
|
#' @param records Exported project records. May be a \code{data.frame} or
|
||||||
#' @param metadata \code{data.frame} containing project metadata (the data dictionary)
|
#' \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
|
#' @author Paul W. Egeler, M.S., GStat
|
||||||
#' @examples
|
#' @examples
|
||||||
#' \dontrun{
|
#' \dontrun{
|
||||||
#' library(jsonlite)
|
|
||||||
#' library(RCurl)
|
#' library(RCurl)
|
||||||
#'
|
#'
|
||||||
#' # Get the metadata
|
#' # Get the metadata
|
||||||
@ -29,21 +31,19 @@
|
|||||||
#' returnFormat = 'json'
|
#' returnFormat = 'json'
|
||||||
#' )
|
#' )
|
||||||
#'
|
#'
|
||||||
#' # Convert JSON to data.frames
|
#' # Convert exported JSON strings into a list of data.frames
|
||||||
#' records <- fromJSON(result.record)
|
|
||||||
#' metadata <- fromJSON(result.meta)
|
|
||||||
#'
|
|
||||||
#' # Split the data.frame into a list of data.frames
|
|
||||||
#' REDCap_split(records, metadata)
|
#' REDCap_split(records, metadata)
|
||||||
#' }
|
#' }
|
||||||
#' @return a list of data.frames
|
#' @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
|
#' @export
|
||||||
REDCap_split <- function(records, metadata) {
|
REDCap_split <- function(records, metadata) {
|
||||||
|
|
||||||
stopifnot(all(sapply(list(records,metadata), inherits, "data.frame")))
|
records <- JSON2data.frame(records)
|
||||||
|
metadata <- JSON2data.frame(metadata)
|
||||||
|
|
||||||
# Check to see if there were any repeating instruments
|
# Check to see if there were any repeating instruments
|
||||||
|
|
||||||
if (!any(names(records) == "redcap_repeat_instrument")) {
|
if (!any(names(records) == "redcap_repeat_instrument")) {
|
||||||
|
|
||||||
message("There are no repeating instruments in this data.")
|
message("There are no repeating instruments in this data.")
|
||||||
@ -109,6 +109,6 @@ REDCap_split <- function(records, metadata) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return(out)
|
out
|
||||||
|
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
Version: 1.0
|
Version: 1.0
|
||||||
|
|
||||||
RestoreWorkspace: Default
|
RestoreWorkspace: No
|
||||||
SaveWorkspace: Default
|
SaveWorkspace: No
|
||||||
AlwaysSaveHistory: Default
|
AlwaysSaveHistory: Default
|
||||||
|
|
||||||
EnableCodeIndexing: Yes
|
EnableCodeIndexing: Yes
|
||||||
@ -14,3 +14,8 @@ LaTeX: pdfLaTeX
|
|||||||
|
|
||||||
AutoAppendNewline: Yes
|
AutoAppendNewline: Yes
|
||||||
StripTrailingWhitespace: 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")
|
12
README.md
12
README.md
@ -35,15 +35,21 @@ a solution to handle the problem in both SAS and R.
|
|||||||
## Instructions
|
## Instructions
|
||||||
### R
|
### R
|
||||||
|
|
||||||
The function definition file contains [roxygen2](https://cran.r-project.org/package=roxygen2) comments to assist you.
|
First you must install the package. To do so, execute the following in your R console:
|
||||||
|
|
||||||
|
```r
|
||||||
|
if (!require(devtools)) install.packages("devtools")
|
||||||
|
devtools::install_github("SpectrumHealthResearch/REDCapRITS/R")
|
||||||
|
```
|
||||||
|
|
||||||
|
After the package is installed, follow these instructions:
|
||||||
|
|
||||||
1. Run the function definition in the source editor or using `source()`.
|
|
||||||
1. Download the record dataset and metadata. This can
|
1. Download the record dataset and metadata. This can
|
||||||
be accomplished either by traditional methods or using the API. The
|
be accomplished either by traditional methods or using the API. The
|
||||||
`read.csv()` function should be able to handle newline characters within
|
`read.csv()` function should be able to handle newline characters within
|
||||||
records, so no pre-processing of metadata csv is needed.
|
records, so no pre-processing of metadata csv is needed.
|
||||||
1. Call the function, pointing it to your record dataset and metadata
|
1. Call the function, pointing it to your record dataset and metadata
|
||||||
`data.frame`s.
|
`data.frame`s or JSON character vectors.
|
||||||
|
|
||||||
### SAS
|
### SAS
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user