fix: keeps labels modifying factors and creates new factors correctly

This commit is contained in:
Andreas Gammelgaard Damsbo 2026-03-11 10:17:42 +01:00
commit 32f299880d
No known key found for this signature in database
23 changed files with 676 additions and 80 deletions

View file

@ -351,29 +351,17 @@ compare_missings <- function(data,
#' ## missings_logic_across() |>
#' ## gtsummary::tbl_summary()
missings_logic_across <- function(data, exclude = NULL) {
# This function includes a approach way to preserve variable labels
# This function includes a way to preserve variable labels
with_labels(data,{
names(data) |>
lapply(\(.x) {
# browser()
# Saving original labels
lab <- REDCapCAST::get_attr(data[[.x]], attr = "label")
if (!.x %in% exclude) {
out <- is.na(data[[.x]])
is.na(data[[.x]])
} else {
out <- data[[.x]]
}
if (!is.na(lab)) {
# Restoring original labels, if not NA
REDCapCAST::set_attr(
data = out,
label = lab,
attr = "label",
overwrite = TRUE
)
} else {
out
data[[.x]]
}
}) |>
dplyr::bind_cols(.name_repair = "unique_quiet") |>
setNames(names(data))
})
}