Skip to content

Commit

Permalink
Fix bug with duplicated column labels when `tab_pivot(stat_position =…
Browse files Browse the repository at this point in the history
… "inside_columns")`. Issue #102
  • Loading branch information
gdemin committed Jun 18, 2023
1 parent 7a790ba commit e719391
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/custom_tables.R
Expand Up @@ -1180,7 +1180,7 @@ pivot_columns = function(data, stat_position = c("inside", "outside"),
labels = data[[STAT_LABELS]]
labels_index = seq_along(labels)

all_colnames = unlist(lapply(results, function(item) colnames(item)[-1]))
all_colnames = unlist(lapply(results, function(item) make.unique(colnames(item)[-1])))
colnames_index = match(all_colnames, unique(all_colnames))
results_ncols = vapply(results, NCOL, FUN.VALUE = numeric(1)) - 1 # 'row_labels' excluded

Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test_subtotal.R
Expand Up @@ -431,3 +431,31 @@ res = structure(list(row_labels = c("Group 1|varA", "Group 1|varB",
if(as.numeric(version$major) >=4){
expect_equal(output, res)
}

context("inside_columns with subtotal issue #102")

data(infert)
res = infert %>%
tab_cells(parity) %>%
tab_cols(
#total(),
subtotal(
education,
"LESS THAN 12 Y.O." = levels(education)[1:2],
"GREATER THAN 5 Y.O." = levels(education)[2:3]
)[,-1]
) %>%
tab_stat_cases() %>%
tab_pivot(
stat_position = "inside_columns"
)

expect_identical(res,
structure(list(row_labels = c("parity|1", "parity|2", "parity|3",
"parity|4", "parity|5", "parity|6", "parity|#Total cases"), `0-5yrs` = c(3,
NA, NA, 3, NA, 6, 12), `6-11yrs` = c(42, 42, 21, 12, 3, NA, 120
), `LESS THAN 12 Y.O.` = c(45, 42, 21, 15, 3, 6, 132), `6-11yrs` = c(42,
42, 21, 12, 3, NA, 120), `12+ yrs` = c(54, 39, 15, 3, 3, 2, 116
), `GREATER THAN 5 Y.O.` = c(96, 81, 36, 15, 6, 2, 236)), row.names = c(NA,
-7L), class = c("etable", "data.frame"))
)

0 comments on commit e719391

Please sign in to comment.