Skip to content

Commit dc505a4

Browse files
committed
fix factor to integer bug
1 parent f390ae2 commit dc505a4

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

R/rename_ard_columns.R

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ rename_ard_columns <- function(x,
125125
dplyr::select(-"...ard_row_order...") |>
126126
dplyr::mutate(
127127
# replace NULL values with NA, then unlist
128-
across(all_of(all_new_names), ~ map(., \(value) value %||% NA) |> unlist())
128+
across(
129+
all_of(all_new_names),
130+
~ map(., \(value){
131+
if (inherits(value, "factor")) value <- as.character(value)
132+
value %||% NA
133+
})|>
134+
unlist())
129135
)
130136
}

tests/testthat/test-rename_ard_columns.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,24 @@ test_that("rename_ard_columns(unlist) lifecycle", {
5353
rename_ard_columns(unlist = "stat")
5454
)
5555
})
56+
57+
58+
test_that("rename_ard_columns() preserves factor levels as characters", {
59+
adsl_ <- ADSL |>
60+
dplyr::mutate(
61+
RACE = factor(RACE, levels = c("WHITE", "BLACK OR AFRICAN AMERICAN", "AMERICAN INDIAN OR ALASKA NATIVE"))
62+
)
63+
64+
res <- ard_tabulate(
65+
data = adsl_,
66+
by = TRT01A,
67+
variables = RACE
68+
) |>
69+
rename_ard_columns()
70+
71+
# Check that 'race' is a character vector
72+
expect_type(res$RACE, "character")
73+
74+
# Check that it contains the actual level string, not an integer "1", "2", etc.
75+
expect_true("WHITE" %in% res$RACE)
76+
})

0 commit comments

Comments
 (0)