R/REDCap_codebook_prepare.R

Defines functions REDCap_codebook_prepare

Documented in REDCap_codebook_prepare

#' Prepare a REDCap codebook for downstream processing
#'
#' Cleans HTML formatting from codebook labels and optionally filters the
#' codebook to selected REDCap forms.
#'
#' @param codebook A REDCap data dictionary as a data frame.
#' @param strings_to_ignore A regular expression used in
#'   [stringr::str_remove_all()] to remove formatting fragments from field labels.
#' @param form.filter Optional character vector of REDCap form names to keep.
#' @param field.label Unquoted column containing question labels.
#' @param form.col Unquoted column containing the form name (for filtering)
#'
#' @return A cleaned codebook tibble/data frame.
#' @export
#'
#' @examples
#' codebook_path <- system.file("ext", "DataDictionary_sleepdiary.csv",
#'   package = "melidosData"
#' )
#' codebook <- utils::read.csv(codebook_path, check.names = FALSE)
#' cleaned <- REDCap_codebook_prepare(codebook)
#' cleaned$`Field Label`
#' #compate the original one
#' codebook$`Field Label`
REDCap_codebook_prepare <- function(codebook,
                                    strings_to_ignore = codebook_strings_to_ignore,
                                    form.filter = NULL,
                                    field.label = `Field Label`,
                                    form.col = `Form Name`
                                    ) {
  if (!is.null(form.filter)) {
    codebook <-
      codebook |>
      dplyr::filter({{ form.col }} %in% form.filter)
  }

  codebook |>
    dplyr::mutate(
      {{ field.label }} :=
        {{ field.label }} |>
        stringr::str_remove_all(strings_to_ignore)
    )
}

Try the melidosData package in your browser

Any scripts or data that you put into this service are public.

melidosData documentation built on April 22, 2026, 5:09 p.m.