Skip to content

Commit

Permalink
Account for ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
edelarua committed Oct 2, 2024
1 parent 9d017bf commit 07200b4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
19 changes: 18 additions & 1 deletion R/brdg_hierarchical.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#' named list of summary types.
#' @param count (scalar `logical`)\cr
#' whether `tbl_hierarchical_count()` (`TRUE`) or `tbl_hierarchical()` (`FALSE`) is being applied.
#' @param is_ordered (scalar `logical`)\cr
#' whether the last variable in `hierarchies` is ordered.
#' @param label (named `list`)\cr
#' named list of hierarchy variable labels.
#' @inheritParams tbl_hierarchical
Expand All @@ -53,6 +55,7 @@ brdg_hierarchical <- function(cards,
type,
overall_row,
count,
is_ordered,
label) {
set_cli_abort_call()

Expand Down Expand Up @@ -81,6 +84,20 @@ brdg_hierarchical <- function(cards,
)
}

if (is_ordered) {
grpX <- paste0("group", n_by + 1)
grpX_lvl <- paste0(grpX, "_level")
cards[which(cards[[grpX]] == hierarchies |> tail(1)), ] <-
cards[which(cards[[grpX]] == hierarchies |> tail(1)), ] |>
dplyr::rename(
variable := !!grpX,
variable_level := !!grpX_lvl,
!!grpX := "variable",
!!grpX_lvl := "variable_level"
) |>
cards::tidy_ard_column_order()
}

table_body <- pier_summary_hierarchical(
cards = cards,
variables = hierarchies,
Expand All @@ -97,7 +114,7 @@ brdg_hierarchical <- function(cards,

# create dummy rows
tbl_rows <- table_body |>
dplyr::filter(across(cards::all_ard_groups("names"), ~ .x != " ")) |>
dplyr::filter(dplyr::if_any(cards::all_ard_groups("names"), ~ .x != " ")) |>
select(row_type, prior_gp, prior_gp_lvl) |>
unique() |>
mutate(
Expand Down
12 changes: 9 additions & 3 deletions R/tbl_hierarchical.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ tbl_hierarchical <- function(data,
denominator = denominator,
include = {{ include }},
statistic = statistic,
overall_row = {{ overall_row }},
overall_row = overall_row,
label = label,
digits = digits
)
Expand Down Expand Up @@ -175,7 +175,7 @@ tbl_hierarchical_count <- function(data,
denominator = denominator,
include = {{ include }},
statistic = "{n}",
overall_row = {{ overall_row }},
overall_row = overall_row,
label = label,
digits = digits
)
Expand Down Expand Up @@ -356,6 +356,7 @@ internal_tbl_hierarchical <- function(data,
type,
overall_row,
count = is_empty(id),
is_ordered = is.ordered(data[[hierarchies |> tail(1)]]),
label
) |>
append(
Expand All @@ -370,6 +371,11 @@ internal_tbl_hierarchical <- function(data,
# this function calculates either the counts or the rates of the events
.run_ard_stack_hierarchical_fun <- function(data, hierarchies, by, id, denominator, include, statistic, overall_row) {
if (!is_empty(id)) {
if (is.ordered(data[[hierarchies |> tail(1)]])) {
by <- c(by, hierarchies |> tail(1))
hierarchies <- hierarchies |> head(-1)
include <- c(intersect(include, hierarchies), hierarchies |> tail(1))
}
cards::ard_stack_hierarchical(
data = data,
variables = hierarchies,
Expand All @@ -379,7 +385,7 @@ internal_tbl_hierarchical <- function(data,
include = include,
statistic = statistic,
over_variables = overall_row,
total_n = TRUE#is_empty(by)
total_n = is_empty(by)
)
} else {
cards::ard_stack_hierarchical_count(
Expand Down
4 changes: 4 additions & 0 deletions man/brdg_hierarchical.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 07200b4

Please sign in to comment.