From 7cae55dba465e373f5a7c1e6a72c592e9253956a Mon Sep 17 00:00:00 2001 From: Andreas Gammelgaard Damsbo Date: Tue, 9 Jan 2024 10:22:49 +0100 Subject: [PATCH] Updated read_redcap_tables to handle non-longitudinal projects and give better error on invalid events or forms --- DESCRIPTION | 7 ++++--- NEWS.md | 6 ++++++ R/read_redcap_tables.R | 24 ++++++++++++++---------- man/read_redcap_tables.Rd | 5 +++-- 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5f60bcc..9dc73d5 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,9 +2,10 @@ Package: REDCapCAST Title: REDCap Castellated Data Handling Version: 23.12.1 Authors@R: c( - person("Andreas Gammelgaard", "Damsbo", email = "agdamsbo@clin.au.dk", role = c("aut", "cre"), - comment = c(ORCID = "0000-0002-7559-1154")), - person("Paul", "Egeler", email = "paul.egeler@spectrumhealth.org", role = "aut")) + person("Andreas Gammelgaard", "Damsbo", email = "agdamsbo@clin.au.dk", + role = c("aut", "cre"),comment = c(ORCID = "0000-0002-7559-1154")), + person("Paul", "Egeler", email = "paulegeler@gmail.com", role = c("aut"), + comment = c(ORCID = "0000-0001-6948-9498"))) Description: Originally forked from the R part of 'REDCapRITS' by Paul Egeler. See . Handles castellated datasets from 'REDCap' projects with repeating instruments. diff --git a/NEWS.md b/NEWS.md index 708c7db..6172559 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,9 @@ +# REDCapCAST 24.1.1 + +### Functions + +* Fix: `read_redcap_tables()`: checking form names based on data dictionary to allow handling of non-longitudinal projects. Prints invalid form names and invalid event names. If invalid form names are supplied to `REDCapR::redcap_read()` (which is the backbone), all forms are exported, which is not what we want with a focused approach. Invalid event names will give an output with a rather peculiar formatting. + # REDCapCAST 23.12.1 One new function to ease secure dataset retrieval and a few bug fixes. diff --git a/R/read_redcap_tables.R b/R/read_redcap_tables.R index 8ef0cb1..4913c12 100644 --- a/R/read_redcap_tables.R +++ b/R/read_redcap_tables.R @@ -1,9 +1,10 @@ #' Download REDCap data #' #' Implementation of REDCap_split with a focused data acquisition approach using -#' REDCapR::redcap_read nad only downloading specified fields, forms and/or +#' REDCapR::redcap_read and only downloading specified fields, forms and/or #' events using the built-in focused_metadata including some clean-up. -#' Works with longitudinal projects with repeating instruments. +#' Works with classical and longitudinal projects with or without repeating +#' instruments. #' @param uri REDCap database uri #' @param token API token #' @param records records to download @@ -39,26 +40,29 @@ read_redcap_tables <- function(uri, )) { - if (!is.null(forms) | !is.null(events)){ - arm_event_inst <- REDCapR::redcap_event_instruments(redcap_uri = uri, - token = token) + m <- REDCapR::redcap_metadata_read(redcap_uri = uri, token = token)[["data"]] - if (!is.null(forms)){ - forms_test <- forms %in% unique(arm_event_inst$data$form) + if (!is.null(forms)){ + + forms_test <- forms %in% unique(m$form_name) if (any(!forms_test)){ + print(paste0("The following form names are invalid: ", paste(forms[!forms_test],collapse=", "),".")) stop("Not all supplied forms are valid") } - } + } + + if (!is.null(events)){ + arm_event_inst <- REDCapR::redcap_event_instruments(redcap_uri = uri, + token = token) - if (!is.null(events)){ event_test <- events %in% unique(arm_event_inst$data$unique_event_name) if (any(!event_test)){ + print(paste0("The following event names are invalid: ", paste(events[!event_test],collapse=", "),".")) stop("Not all supplied event names are valid") } - } } # Getting dataset diff --git a/man/read_redcap_tables.Rd b/man/read_redcap_tables.Rd index 0522049..c2c36a8 100644 --- a/man/read_redcap_tables.Rd +++ b/man/read_redcap_tables.Rd @@ -43,9 +43,10 @@ list of instruments } \description{ Implementation of REDCap_split with a focused data acquisition approach using -REDCapR::redcap_read nad only downloading specified fields, forms and/or +REDCapR::redcap_read and only downloading specified fields, forms and/or events using the built-in focused_metadata including some clean-up. -Works with longitudinal projects with repeating instruments. +Works with classical and longitudinal projects with or without repeating +instruments. } \examples{ # Examples will be provided later