-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathheader-transform.R
132 lines (124 loc) · 2.92 KB
/
header-transform.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
fill_header <- function(x, fill = TRUE) {
if (!fill) {
return(x)
}
x %>%
tidyr::pivot_longer(
tidyselect::starts_with("level"),
names_to = "var",
names_prefix = "level",
values_to = "val"
) %>%
tidyr::fill("val") %>%
tidyr::pivot_wider(
id_cols = "original",
names_from = "var", values_from = "val", names_prefix = "level"
)
}
merge_header <- function(x, merge = TRUE) {
if (!merge) {
return(x)
}
x %>%
flextable::merge_h(part = "header") %>%
flextable::merge_v(part = "header")
}
transform_header <- function(
x,
sep = "[_\\.]",
theme_fun = NULL,
.fill = FALSE,
.merge = FALSE,
...) {
header <- names(x$header$dataset)
mapping <- data.frame(original = header, stringsAsFactors = FALSE) %>%
tidyr::separate(
"original",
paste0("level", seq(max(stringr::str_count(header, sep) + 1))),
sep = sep, fill = "right", remove = FALSE
) %>%
fill_header(.fill)
if (is.null(theme_fun)) {
default_theme <- flextable::get_flextable_defaults()$theme_fun
theme_fun <- if (is.function(default_theme)) {
default_theme
} else {
getNamespace("flextable")[[default_theme]]
}
}
x %>%
flextable::set_header_df(mapping, key = "original") %>%
merge_header(.merge) %>%
theme_fun(...) %>%
flextable::fix_border_issues()
}
#' Split the header based on delimiters
#'
#' @note
#' `split_header` is a rename of `separate_header` and the latter will be removed in the future release.
#'
#' @param x A `flextable` object`
#' @inheritParams tidyr::separate
#' @inheritParams flextable::flextable
#' @param
#' theme_fun A flextable theme function.
#' When `NULL` (default), the value is resolved by
#' `flextable::get_flextable_defaults()`.
#' @param ... Passed to `theme_fun`
#'
#' @examples
#' iris %>%
#' flextable() %>%
#' separate_header()
#' @export
split_header <- function(
x,
sep = "[_.]",
theme_fun = NULL,
...) {
transform_header(
x,
sep = sep, theme_fun = theme_fun, .fill = FALSE, .merge = FALSE, ...
)
}
#' @rdname split_header
#' @export
separate_header <- function(
x,
sep = "[_.]",
theme_fun = NULL,
...) {
.Deprecated(
"split_header",
msg = paste(
"ftExtra::separate_header will be removed in the future release",
"to avoid masking `flextable::separate_header`.",
"Instead, use ftExtra::split_header which is a copy of",
"ftExtra::separate_header."
)
)
split_header(x, sep, theme_fun, ...)
}
#' Span the header based on delimiters
#'
#' @inherit separate_header
#' @inheritParams tidyr::separate
#' @inheritParams flextable::flextable
#'
#' @examples
#' iris %>%
#' flextable() %>%
#' span_header()
#' @export
span_header <- function(
x,
sep = "[_.]",
theme_fun = NULL,
...) {
transform_header(
x,
sep = sep, theme_fun = theme_fun,
.fill = TRUE, .merge = TRUE,
...
)
}