forked from ddsjoberg/gtsummary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbl_split.R
71 lines (67 loc) · 2.11 KB
/
tbl_split.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#' Split gtsummary table
#'
#' \lifecycle{experimental}
#' The `tbl_split` function splits a single gtsummary table into multiple tables.
#' Updates to the print method are expected.
#'
#' @param x gtsummary table
#' @param variables variables at which to split the gtsummary table rows (tables
#' will be separated after each of these variables)
#' @param ... not used
#'
#' @return `tbl_split` object
#' @family tbl_regression tools
#' @family tbl_uvregression tools
#' @family tbl_summary tools
#' @family tbl_survfit tools
#' @family tbl_svysummary tools
#'
#' @examples
#' tbl <-
#' tbl_summary(trial) %>%
#' tbl_split(variables = c(marker, grade))
#'
#' @name tbl_split
NULL
#' @export
#' @rdname tbl_split
tbl_split <- function(x, ...) {
UseMethod("tbl_split")
}
#' @export
#' @rdname tbl_split
tbl_split.gtsummary <- function(x, variables, ...) {
check_dots_empty(error = function(e) inform(c(e$message, e$body)))
# check/parse inputs ---------------------------------------------------------
variables <-
broom.helpers::.select_to_varnames(
{{ variables }},
var_info = x$table_body,
arg_name = "variable"
) %>%
# adding last variable
union(x$table_body$variable %>% rev() %>% purrr::pluck(1))
# merging split points -------------------------------------------------------
# convert list of table_body into list of gtsummary objects
x$table_body %>%
dplyr::left_join(
tibble(variable = variables, ..group.. = variables),
by = "variable"
) %>%
tidyr::fill("..group..", .direction = "up") %>%
tidyr::nest(data = -"..group..") %>%
dplyr::pull("data") %>%
purrr::map(
~ list(.) %>%
purrr::set_names("table_body") %>%
c(purrr::list_modify(x, "table_body" = NULL)) %>% # add the other parts of the gtsummary table
`class<-`(class(x)) # add original class from `x`
) %>%
`class<-`("tbl_split") # assign class (can't assign gtsummary because of print definitions)
}
#' @export
#' @rdname tbl_split
print.tbl_split <- function(x, ...) {
check_dots_empty(error = function(e) inform(c(e$message, e$body)))
purrr::walk(x, print)
}