REDCapCAST/R/read_redcap_tables.R

239 lines
6.6 KiB
R
Raw Normal View History

2023-02-28 09:54:03 +01:00
#' Download REDCap data
#'
2024-11-28 14:31:27 +01:00
#' @description
#' Implementation of passed on to \link[REDCapCAST]{REDCap_split} with a focused
#' data acquisition approach using passed on to \link[REDCapR]{redcap_read} and
#' only downloading specified fields, forms and/or events using the built-in
#' focused_metadata including some clean-up.
#' Works with classical and longitudinal projects with or without repeating
#' instruments.
2024-11-28 14:31:27 +01:00
#' Will preserve metadata in the data.frames as labels.
#'
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
2024-11-28 14:31:27 +01:00
#' @param raw_or_label raw or label tags. Can be "raw", "label" or "both".
#'
2024-11-28 14:31:27 +01:00
#' * "raw": Standard \link[REDCapR]{redcap_read} method to get raw values.
#' * "label": Standard \link[REDCapR]{redcap_read} method to get label values.
#' * "both": Get raw values with REDCap labels applied as labels. Use
2024-11-28 14:31:27 +01:00
#' \link[REDCapCAST]{as_factor} to format factors with original labels and use
#' the `gtsummary` package functions like \link[gtsummary]{tbl_summary} to
#' easily get beautiful tables with original labels from REDCap. Use
#' \link[REDCapCAST]{fct_drop} to drop empty levels.
#'
2023-04-13 10:57:04 +02:00
#' @param split_forms Whether to split "repeating" or "all" forms, default is
#' all.
2024-11-28 14:31:27 +01:00
#' @param ... passed on to \link[REDCapR]{redcap_read}
2023-02-28 09:54:03 +01:00
#'
#' @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,
2024-11-28 14:31:27 +01:00
raw_or_label = c("raw","label","both"),
split_forms = "all",
...) {
raw_or_label <- match.arg(raw_or_label, c("raw","label","both"))
2024-01-09 11:06:31 +01:00
# Getting metadata
m <-
REDCapR::redcap_metadata_read(redcap_uri = uri, token = token)[["data"]]
if (!is.null(fields)) {
2024-04-09 10:57:29 +02:00
fields_test <- fields %in% c(m$field_name,paste0(unique(m$form_name),"_complete"))
if (any(!fields_test)) {
2024-02-27 13:20:21 +01:00
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
if (!is.null(forms)) {
forms_test <- forms %in% unique(m$form_name)
2023-03-06 14:37:06 +01:00
if (any(!forms_test)) {
2024-02-27 13:20:21 +01:00
print(paste0("The following form names are invalid: ",
paste(forms[!forms_test], collapse = ", "), "."))
stop("Not all supplied form names are valid")
2023-03-06 14:37:06 +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)
if (any(!event_test)) {
2024-02-27 13:20:21 +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
if (raw_or_label=="both"){
rorl <- "raw"
} else {
rorl <- raw_or_label
}
# 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,
2024-11-28 14:31:27 +01:00
raw_or_label = rorl,
...
)[["data"]]
2023-02-28 09:54:03 +01:00
if (raw_or_label=="both"){
d <- apply_field_label(data=d,meta=m)
d <- apply_factor_labels(data=d,meta=m)
}
# 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.
if ("redcap_repeat_instrument" %in% names(d)) {
d$redcap_repeat_instrument <- clean_redcap_name(d$redcap_repeat_instrument)
}
2024-01-09 11:06:31 +01:00
# Processing metadata to reflect focused dataset
m <- focused_metadata(m, names(d))
2024-02-27 13:20:21 +01:00
# Splitting
out <- REDCap_split(d,
m,
forms = split_forms,
primary_table_name = ""
)
2023-02-28 09:54:03 +01:00
2024-02-27 13:20:21 +01:00
sanitize_split(out)
2023-02-28 09:54:03 +01:00
}
#' Very simple function to remove rich text formatting from field label
#' and save the first paragraph ('<p>...</p>').
#'
#' @param data field label
#'
#' @return character vector
#' @export
#'
#' @examples
#' clean_field_label("<div class=\"rich-text-field-label\"><p>Fazekas score</p></div>")
clean_field_label <- function(data) {
out <- data |>
lapply(\(.x){
unlist(strsplit(.x, "</"))[1]
}) |>
lapply(\(.x){
splt <- unlist(strsplit(.x, ">"))
splt[length(splt)]
})
Reduce(c, out)
}
2024-11-28 14:31:27 +01:00
#' Converts REDCap choices to factor levels and stores in labels attribute
#'
#' @description
#' Applying \link[REDCapCAST]{as_factor} to the data.frame or variable, will
#' coerce to a factor.
#'
#' @param data vector
#' @param meta vector of REDCap choices
#'
#' @return vector of class "labelled" with a "labels" attribute
#' @export
#'
#' @examples
#' format_redcap_factor(sample(1:3,20,TRUE),"1, First. | 2, second | 3, THIRD")
format_redcap_factor <- function(data, meta) {
lvls <- strsplit(meta, " | ", fixed = TRUE) |>
unlist() |>
lapply(\(.x){
splt <- unlist(strsplit(.x, ", "))
stats::setNames(splt[1], nm = paste(splt[-1], collapse = ", "))
}) |>
(\(.x){
Reduce(c, .x)
})()
set_attr(data, label = lvls, attr = "labels") |>
2024-11-28 14:31:27 +01:00
set_attr(data, label = "labelled", attr = "class")
}
#' Apply REDCap filed labels to data frame
#'
#' @param data REDCap exported data set
#' @param meta REDCap data dictionary
#'
#' @return data.frame
#' @export
#'
apply_field_label <- function(data,meta){
purrr::imap(data, \(.x, .i){
if (.i %in% meta$field_name) {
# Does not handle checkboxes
out <- set_attr(.x,
label = clean_field_label(meta$field_label[meta$field_name == .i]),
attr = "label"
)
out
} else {
.x
}
}) |> dplyr::bind_cols()
}
#' Preserve all factor levels from REDCap data dictionary in data export
#'
#' @param data REDCap exported data set
#' @param meta REDCap data dictionary
#'
#' @return data.frame
#' @export
#'
apply_factor_labels <- function(data,meta=NULL){
2024-12-04 08:05:45 +01:00
if (is.list(data) && !is.data.frame(data)){
meta <- data$meta
data <- data$data
} else if (is.null(meta)) {
stop("Please provide a data frame for meta")
}
purrr::imap(data, \(.x, .i){
if (any(c("radio", "dropdown") %in% meta$field_type[meta$field_name == .i]) || is.factor(.x)) {
format_redcap_factor(.x, meta$select_choices_or_calculations[meta$field_name == .i])
} else {
.x
}
}) |> dplyr::bind_cols()
}