From b57e130395d8c7cf4c26430465af2bbfdeaa7b5d Mon Sep 17 00:00:00 2001 From: AG Damsbo Date: Mon, 6 Mar 2023 14:37:06 +0100 Subject: [PATCH] new tests and removed notes --- R/read_redcap_tables.R | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/R/read_redcap_tables.R b/R/read_redcap_tables.R index 4d63913..b2e3afd 100644 --- a/R/read_redcap_tables.R +++ b/R/read_redcap_tables.R @@ -12,10 +12,10 @@ #' @param raw_or_label raw or label tags #' @param generics vector of auto-generated generic variable names to #' ignore when discarding empty rows -#' @param ... extra parameters for internal REDCapR::redcap_read #' #' @return list of instruments -#' @importFrom REDCapR redcap_metadata_read redcap_read +#' @importFrom REDCapR redcap_metadata_read redcap_read redcap_event_instruments +#' @include utils.r #' @export #' #' @examples @@ -32,12 +32,30 @@ read_redcap_tables <- function(uri, "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. + )) { + + + if (!is.null(forms) | !is.null(events)){ + arm_event_inst <- REDCapR::redcap_event_instruments(redcap_uri = uri, + token = token) + + if (!is.null(forms)){ + + forms_test <- forms %in% unique(arm_event_inst$data$form) + + if (any(!forms_test)){ + stop("Not all supplied forms are valid") + } + } + + if (!is.null(events)){ + event_test <- events %in% unique(arm_event_inst$data$unique_event_name) + + if (any(!forms_test)){ + stop("Not all supplied event names are valid") + } + } + } d <- REDCapR::redcap_read( redcap_uri = uri, @@ -46,15 +64,14 @@ read_redcap_tables <- function(uri, events = events, forms = forms, records = records, - raw_or_label = raw_or_label, - ... + 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), ], + focused_metadata(m$data,names(d$data)), forms = "all") lapply(l, function(i) {