diff --git a/DESCRIPTION b/DESCRIPTION index ed9953c..a7df534 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,11 +1,10 @@ Package: REDCapRITS Title: REDCap Repeating Instrument Table Splitter Fork -Version: 0.2.2.1 +Version: 0.2.3 Authors@R: c( person("Paul", "Egeler", email = "paul.egeler@spectrumhealth.org", role = c("aut")), person("Andreas Gammelgaard", "Damsbo", email = "agdamsbo@clin.au.dk", role = c("cre", "ctb","cph"), comment = c(ORCID = "0000-0002-7559-1154"))) -Copyright: Spectrum Health, Grand Rapids, MI Description: This is a fork of REDCapRITS by Paul Egeler and Spectrum Health. See [https://github.com/SpectrumHealthResearch/REDCapRITS](https://github.com/SpectrumHealthResearch/REDCapRITS). Split REDCap repeating instruments output into multiple tables. @@ -32,7 +31,10 @@ LazyData: true RoxygenNote: 7.2.3 URL: https://github.com/agdamsbo/REDCapRITS BugReports: https://github.com/agdamsbo/REDCapRITS/issues +Imports: + REDCapR Collate: 'utils.r' 'process_user_input.r' 'REDCap_split.r' + 'read_redcap_tables.R' diff --git a/NAMESPACE b/NAMESPACE index b1bb9bf..bfef3f5 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,3 +1,6 @@ # Generated by roxygen2: do not edit by hand export(REDCap_split) +export(read_redcap_tables) +importFrom(REDCapR,redcap_metadata_read) +importFrom(REDCapR,redcap_read) diff --git a/R/read_redcap_tables.R b/R/read_redcap_tables.R new file mode 100644 index 0000000..e2ee483 --- /dev/null +++ b/R/read_redcap_tables.R @@ -0,0 +1,71 @@ +#' Download REDCap data +#' +#' Wrapper function for using REDCapR::redcap_read and REDCapRITS::REDCap_split +#' including some clean-up. Works with longitudinal projects with repeating +#' instruments. +#' @param uri REDCap database uri +#' @param token API token +#' @param records records to download +#' @param fields fields to download +#' @param events events to download +#' @param forms forms to download +#' @param raw_or_label raw or label tags +#' @param generics vector of auto-generated generic variable names to +#' ignore when discarding empty rows +#' @param ... ekstra parameters for REDCapR::redcap_read_oneshot +#' +#' @return list of instruments +#' @importFrom REDCapR redcap_metadata_read redcap_read +#' @export +#' +#' @examples +#' # Examples will be provided later +read_redcap_tables <- function(uri, + token, + records = NULL, + fields = NULL, + events = NULL, + forms = NULL, + raw_or_label = "label", + generics = c( + "record_id", + "redcap_event_name", + "redcap_repeat_instrument", + "redcap_repeat_instance" + ), + ...) { + # Notes to self: Based on the metadata, this functionality could be + # introduced without using the REDCapRITS package.. To be tried.. + # + # This does not handle repeated instruments!! This should be implemented. + + d <- REDCapR::redcap_read_oneshot( + redcap_uri = uri, + token = token, + fields = fields, + events = events, + forms = forms, + records = records, + raw_or_label = raw_or_label, + ... + ) + + m <- + REDCapR::redcap_metadata_read (redcap_uri = uri, token = token) + + l <- REDCap_split(d$data, + m$data[m$data$field_name %in% names(d$data), ], + forms = "all") + + lapply(l, function(i) { + if (ncol(i) > 2) { + s <- data.frame(i[, !colnames(i) %in% generics]) + i[!apply(is.na(s), MARGIN = 1, FUN = all), ] + } else { + i + } + }) + +} + + diff --git a/man/read_redcap_tables.Rd b/man/read_redcap_tables.Rd new file mode 100644 index 0000000..31cbfa5 --- /dev/null +++ b/man/read_redcap_tables.Rd @@ -0,0 +1,50 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/read_redcap_tables.R +\name{read_redcap_tables} +\alias{read_redcap_tables} +\title{Download REDCap data} +\usage{ +read_redcap_tables( + uri, + token, + records = NULL, + fields = NULL, + events = NULL, + forms = NULL, + raw_or_label = "label", + generics = c("record_id", "redcap_event_name", "redcap_repeat_instrument", + "redcap_repeat_instance"), + ... +) +} +\arguments{ +\item{uri}{REDCap database uri} + +\item{token}{API token} + +\item{records}{records to download} + +\item{fields}{fields to download} + +\item{events}{events to download} + +\item{forms}{forms to download} + +\item{raw_or_label}{raw or label tags} + +\item{generics}{vector of auto-generated generic variable names to +ignore when discarding empty rows} + +\item{...}{ekstra parameters for REDCapR::redcap_read_oneshot} +} +\value{ +list of instruments +} +\description{ +Wrapper function for using REDCapR::redcap_read and REDCapRITS::REDCap_split +including some clean-up. Works with longitudinal projects with repeating +instruments. +} +\examples{ +# Examples will be provided later +} diff --git a/tests/testthat/test-read_redcap_tables.R b/tests/testthat/test-read_redcap_tables.R new file mode 100644 index 0000000..0810dd4 --- /dev/null +++ b/tests/testthat/test-read_redcap_tables.R @@ -0,0 +1,7 @@ +# Unit Test + +# Test that the function throws an error when uri and token are not provided +test_that("read_redcap_tables throws error when uri and token are not provided", + { + testthat::expect_error(read_redcap_tables(uri, token)) + })