2023-02-28 09:54:03 +01:00
|
|
|
#' Download REDCap data
|
|
|
|
#'
|
2023-03-07 15:38:28 +01:00
|
|
|
#' Implementation of REDCap_split with a focused data acquisition approach using
|
2024-01-09 10:22:49 +01:00
|
|
|
#' REDCapR::redcap_read and only downloading specified fields, forms and/or
|
2023-04-13 10:57:04 +02:00
|
|
|
#' events using the built-in focused_metadata including some clean-up.
|
2024-01-09 10:22:49 +01:00
|
|
|
#' Works with classical and longitudinal projects with or without repeating
|
|
|
|
#' instruments.
|
2024-02-05 14:46:37 +01:00
|
|
|
#' @param uri REDCap database API uri
|
2023-02-28 09:54:03 +01:00
|
|
|
#' @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
|
2023-04-13 10:57:04 +02:00
|
|
|
#' @param split_forms Whether to split "repeating" or "all" forms, default is
|
|
|
|
#' all.
|
2023-02-28 09:54:03 +01:00
|
|
|
#' @param generics vector of auto-generated generic variable names to
|
|
|
|
#' ignore when discarding empty rows
|
|
|
|
#'
|
|
|
|
#' @return list of instruments
|
2023-03-06 14:37:06 +01:00
|
|
|
#' @importFrom REDCapR redcap_metadata_read redcap_read redcap_event_instruments
|
|
|
|
#' @include utils.r
|
2023-02-28 09:54:03 +01:00
|
|
|
#' @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",
|
2023-03-07 15:38:28 +01:00
|
|
|
split_forms = "all",
|
2023-02-28 09:54:03 +01:00
|
|
|
generics = c(
|
|
|
|
"record_id",
|
|
|
|
"redcap_event_name",
|
|
|
|
"redcap_repeat_instrument",
|
|
|
|
"redcap_repeat_instance"
|
2023-03-06 14:37:06 +01:00
|
|
|
)) {
|
|
|
|
|
|
|
|
|
2024-01-09 11:06:31 +01:00
|
|
|
# Getting metadata
|
|
|
|
m <-
|
|
|
|
REDCapR::redcap_metadata_read (redcap_uri = uri, token = token)[["data"]]
|
2024-01-09 10:22:49 +01:00
|
|
|
|
2024-01-09 10:27:37 +01:00
|
|
|
if (!is.null(fields)){
|
|
|
|
|
|
|
|
fields_test <- fields %in% unique(m$field_name)
|
|
|
|
|
|
|
|
if (any(!fields_test)){
|
|
|
|
print(paste0("The following field names are invalid: ", paste(fields[!fields_test],collapse=", "),"."))
|
|
|
|
stop("Not all supplied field names are valid")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 14:37:06 +01:00
|
|
|
|
2024-01-09 10:22:49 +01:00
|
|
|
if (!is.null(forms)){
|
2023-03-06 14:37:06 +01:00
|
|
|
|
2024-01-09 10:22:49 +01:00
|
|
|
forms_test <- forms %in% unique(m$form_name)
|
2023-03-06 14:37:06 +01:00
|
|
|
|
|
|
|
if (any(!forms_test)){
|
2024-01-09 10:22:49 +01:00
|
|
|
print(paste0("The following form names are invalid: ", paste(forms[!forms_test],collapse=", "),"."))
|
2024-01-09 10:27:37 +01:00
|
|
|
stop("Not all supplied form names are valid")
|
2023-03-06 14:37:06 +01:00
|
|
|
}
|
2024-01-09 10:22:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is.null(events)){
|
|
|
|
arm_event_inst <- REDCapR::redcap_event_instruments(redcap_uri = uri,
|
|
|
|
token = token)
|
2023-03-06 14:37:06 +01:00
|
|
|
|
|
|
|
event_test <- events %in% unique(arm_event_inst$data$unique_event_name)
|
|
|
|
|
2023-12-20 09:01:40 +01:00
|
|
|
if (any(!event_test)){
|
2024-01-09 10:22:49 +01:00
|
|
|
print(paste0("The following event names are invalid: ", paste(events[!event_test],collapse=", "),"."))
|
2023-03-06 14:37:06 +01:00
|
|
|
stop("Not all supplied event names are valid")
|
|
|
|
}
|
|
|
|
}
|
2023-02-28 09:54:03 +01:00
|
|
|
|
2023-03-07 15:38:28 +01:00
|
|
|
# Getting dataset
|
2023-02-28 13:59:45 +01:00
|
|
|
d <- REDCapR::redcap_read(
|
2023-02-28 09:54:03 +01:00
|
|
|
redcap_uri = uri,
|
|
|
|
token = token,
|
|
|
|
fields = fields,
|
|
|
|
events = events,
|
|
|
|
forms = forms,
|
|
|
|
records = records,
|
2023-03-06 14:37:06 +01:00
|
|
|
raw_or_label = raw_or_label
|
2023-03-07 15:38:28 +01:00
|
|
|
)[["data"]]
|
2023-02-28 09:54:03 +01:00
|
|
|
|
2023-03-07 15:38:28 +01:00
|
|
|
# Process repeat instrument naming
|
2023-04-13 10:57:04 +02:00
|
|
|
# Removes any extra characters other than a-z, 0-9 and "_", to mimic raw
|
|
|
|
# instrument names.
|
2023-03-07 15:38:28 +01:00
|
|
|
if ("redcap_repeat_instrument" %in% names(d)) {
|
2023-04-14 11:46:09 +02:00
|
|
|
d$redcap_repeat_instrument <- clean_redcap_name(d$redcap_repeat_instrument)
|
2023-03-07 15:38:28 +01:00
|
|
|
}
|
|
|
|
|
2024-01-09 11:06:31 +01:00
|
|
|
# Processing metadata to reflect focused dataset
|
2023-03-07 15:38:28 +01:00
|
|
|
if (!is.null(c(fields,forms,events))){
|
|
|
|
m <- focused_metadata(m,names(d))
|
|
|
|
}
|
|
|
|
|
|
|
|
# Splitting
|
|
|
|
l <- REDCap_split(d,
|
|
|
|
m,
|
|
|
|
forms = split_forms,
|
2023-04-14 11:46:09 +02:00
|
|
|
primary_table_name = "")
|
2023-03-07 15:38:28 +01:00
|
|
|
|
|
|
|
# Sanitizing split list by removing completely empty rows apart from colnames
|
|
|
|
# in "generics"
|
|
|
|
sanitize_split(l,generics)
|
2023-02-28 09:54:03 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|