forked from ddsjoberg/gtsummary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils-gtsummary_core.R
154 lines (137 loc) · 4.77 KB
/
utils-gtsummary_core.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# THIS FILE CONTAINS A FEW SCRIPTS THAT ASSIST IN SETTING UP A GENERAL
# GTSUMMARY OBJECT. IF YOU'RE CREATING A GTSUMMARY-LIKE FUNCTION, YOU'LL
# WANT TO GRAB A COPY OF THIS FILE AND PLACE IT IN YOUR PACKAGE.
# LAST UPDATED: 2020-12-19
# .create_gtsummary_object -----------------------------------------------------
#' Function uses `table_body` to create a gtsummary object
#'
#' @param table_body the table_body tibble
#' @param ... other objects that will be added to the gtsummary object list
#'
#' @export
#' @keywords internal
#' @return gtsummary object
.create_gtsummary_object <- function(table_body, ...) {
x <- list() # empty gtsummary object
# table_body -----------------------------------------------------------------
x$table_body <- table_body
# table_styling --------------------------------------------------------------
x$table_styling$header <-
tibble(
column = names(x$table_body),
hide = TRUE,
align = "center",
interpret_label = "gt::md",
label = names(x$table_body),
interpret_spanning_header = "gt::md",
spanning_header = NA_character_
) %>%
mutate(
hide = ifelse(.data$column == "label", FALSE, .data$hide),
align = ifelse(.data$column == "label", "left", .data$align)
)
x$table_styling$footnote <-
tibble(
column = character(), rows = list(),
text_interpret = character(), footnote = character()
)
x$table_styling$footnote_abbrev <-
tibble(
column = character(), rows = list(),
text_interpret = character(), footnote = character()
)
x$table_styling$text_format <-
.purrr_when(
isTRUE(all(c("label", "row_type") %in% x$table_styling$header$column)) ~
tibble(
column = "label",
rows = list(rlang::expr(.data$row_type %in% c("level", "missing"))),
format_type = "indent", undo_text_format = FALSE
),
isFALSE(all(c("label", "row_type") %in% x$table_styling$header$column)) ~
tibble(
column = character(), rows = list(),
format_type = character(), undo_text_format = logical()
)
)
x$table_styling$fmt_missing <-
tibble(column = character(), rows = list(), symbol = character())
x$table_styling$fmt_fun <-
tibble(column = character(), rows = list(), fmt_fun = list())
x$table_styling$cols_merge <-
tibble(column = character(), rows = list(), pattern = character())
# adding other objects to list -----------------------------------------------
x <- c(x, list(...))
# returning gtsummary object -------------------------------------------------
class(x) <- "gtsummary"
x
}
# this fn updates `table_styling` list to match `table_body`
.update_table_styling <- function(x) {
# vector of columns deleted in update
deleted_columns <-
x$table_styling$header$column %>%
setdiff(names(x$table_body))
# if a column was deleted, omit all styling instructions for that column -----
if (!is_empty(deleted_columns)) {
for (styling_element in names(x$table_styling)) {
# if element is a tibble with a column called 'column'
if (is.data.frame(x$table_styling[[styling_element]]) &&
"column" %in% names(x$table_styling[[styling_element]])) {
x$table_styling[[styling_element]] <-
x$table_styling[[styling_element]] %>%
filter(!.data$column %in% deleted_columns)
}
}
}
# update styling header table with new variables -----------------------------
x$table_styling$header <-
tibble(
column = names(x$table_body),
hide = TRUE,
align = "center",
interpret_label = "gt::md",
label = names(x$table_body),
interpret_spanning_header = "gt::md",
spanning_header = NA_character_
) %>%
.rows_update_table_styling_header(x$table_styling$header)
# return x -------------------------------------------------------------------
x
}
.rows_update_table_styling_header <- function(x, y) {
common_columns <- intersect(names(x), names(y))
x %>%
# updating rows in header
dplyr::rows_update(
y %>% select(all_of(common_columns)),
by = "column"
) %>%
# re-adding the columns not in the original header table
dplyr::left_join(
y %>% select(-all_of(setdiff(common_columns, "column"))),
by = "column"
)
}
.purrr_when <- function(...) {
lst_formulas <- rlang::dots_list(...)
for (i in seq_len(length(lst_formulas))) {
if (isTRUE(eval_tidy(.f_lhs_as_quo(lst_formulas[[i]])))) {
return(eval_tidy(.f_rhs_as_quo(lst_formulas[[i]])))
}
}
# if not matches, return NULL
NULL
}
.f_lhs_as_quo <- function(x) {
rlang::new_quosure(
expr = rlang::f_lhs(x),
env = attr(x, ".Environment")
)
}
.f_rhs_as_quo <- function(x) {
rlang::new_quosure(
expr = rlang::f_rhs(x),
env = attr(x, ".Environment")
)
}