-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathdt_stub_df.R
228 lines (172 loc) · 5.33 KB
/
dt_stub_df.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#------------------------------------------------------------------------------#
#
# /$$
# | $$
# /$$$$$$ /$$$$$$
# /$$__ $$|_ $$_/
# | $$ \ $$ | $$
# | $$ | $$ | $$ /$$
# | $$$$$$$ | $$$$/
# \____ $$ \___/
# /$$ \ $$
# | $$$$$$/
# \______/
#
# This file is part of the 'rstudio/gt' project.
#
# Copyright (c) 2018-2024 gt authors
#
# For full copyright and license information, please look at
# https://gt.rstudio.com/LICENSE.html
#
#------------------------------------------------------------------------------#
.dt_stub_df_key <- "_stub_df"
dt_stub_df_get <- function(data) {
dt__get(data, .dt_stub_df_key)
}
dt_stub_df_set <- function(data, stub_df) {
dt__set(data, .dt_stub_df_key, stub_df)
}
dt_stub_df_init <- function(
data,
rowname_col,
groupname_col,
row_group.sep,
process_md
) {
data_tbl <- dt_data_get(data = data)
# Create the `stub_df` table
stub_df <-
vctrs::data_frame(
rownum_i = seq_len(nrow(data_tbl)),
row_id = rep_len(NA_character_, nrow(data_tbl)),
group_id = rep_len(NA_character_, nrow(data_tbl)),
group_label = rep(list(NULL), nrow(data_tbl)),
indent = rep_len(NA_character_, nrow(data_tbl)),
built_group_label = rep_len(NA_character_, nrow(data_tbl))
)
# Handle case where table has no columns
if (ncol(data_tbl) < 1L) {
data <- dt_stub_df_set(data = data, stub_df = stub_df)
return(data)
}
#
# Handle column of data specified as the `rowname_col`
#
if (!is.null(rowname_col) && rowname_col %in% colnames(data_tbl)) {
data <- dt_boxhead_set_stub(data = data, var = rowname_col)
rownames <- data_tbl[[rowname_col]]
row_id <- create_unique_id_vals(rownames, simplify = process_md)
# Place the `row_id` values into `stub_df$row_id`
stub_df[["row_id"]] <- row_id
}
#
# Handle column of data specified as the `groupname_col`
#
if (
!is.null(groupname_col) &&
length(groupname_col) > 0L &&
all(groupname_col %in% colnames(data_tbl))
) {
row_group_labels <-
apply(
data_tbl[, groupname_col],
MARGIN = 1,
FUN = paste,
collapse = row_group.sep
)
if (process_md) {
#
# Ensure that the `row_group_ids` values are simplified to reduce
# special characters; this requires use of the recoding so that the
# generated IDs map correctly to the supplied labels
#
unique_row_group_labels <- unique(row_group_labels)
unique_row_group_ids <-
create_unique_id_vals(unique_row_group_labels, simplify = process_md)
names(unique_row_group_ids) <- unique_row_group_labels
# dplyr::recode is superseded, and is slower now.
# TODO consider using vctrs::vec_case_match when available r-lib/vctrs#1622
row_group_ids <- dplyr::recode(row_group_labels, !!!unique_row_group_ids)
} else {
row_group_ids <- row_group_labels
}
# Place the `row_group_ids` values into `stub_df$group_id`
stub_df[["group_id"]] <- row_group_ids
if (process_md) {
stub_df$group_label <-
lapply(
seq_along(row_group_labels),
FUN = function(x) md(row_group_labels[x])
)
} else {
stub_df[["group_label"]] <- as.list(row_group_labels)
}
data <- dt_boxhead_set_row_group(data = data, vars = groupname_col)
}
# Stop if input `data` has no columns (after modifying `data` for groups)
if (ncol(data_tbl) == 0L) {
cli::cli_abort(
"The `data` must have at least one column that isn't a 'group' column."
)
}
dt_stub_df_set(data = data, stub_df = stub_df)
}
dt_stub_df_exists <- function(data) {
stub_df <- dt_stub_df_get(data = data)
!all(is.na((stub_df)[["row_id"]]))
}
dt_stub_df_build <- function(data, context) {
stub_df <- dt_stub_df_get(data = data)
stub_df$built_group_label <-
vapply(
stub_df$group_label,
FUN.VALUE = character(1L),
FUN = function(label) {
if (!is.null(label)) {
process_text(text = label, context = context)
} else {
""
}
}
)
dt_stub_df_set(data = data, stub_df = stub_df)
}
# Function to obtain a reordered version of `stub_df`
reorder_stub_df <- function(data) {
stub_df <- dt_stub_df_get(data = data)
row_groups <- dt_row_groups_get(data = data)
rows_df <-
get_row_reorder_df(
groups = row_groups,
stub_df = stub_df
)
stub_df <- stub_df[rows_df$rownum_final, ]
dt_stub_df_set(data = data, stub_df = stub_df)
}
dt_stub_groupname_has_na <- function(data) {
stub_df <- dt_stub_df_get(data = data)
anyNA(stub_df$group_id)
}
dt_stub_components <- function(data) {
stub_df <- dt_stub_df_get(data = data)
stub_components <- NULL # same as c()
if (!all(is.na(stub_df[["group_id"]]))) {
stub_components <- c(stub_components, "group_id")
}
# check if some row_id are present and have non-empty chr
if (!all(is.na(stub_df[["row_id"]])) && any(nzchar(stub_df[["row_id"]]))) {
stub_components <- c(stub_components, "row_id")
}
stub_components
}
dt_stub_components_has_rowname <- function(stub_components) {
isTRUE("row_id" %in% stub_components)
}
dt_stub_indentation_at_position <- function(data, i) {
indent_i <- dt_stub_df_get(data = data)$indent[[i]]
if (is.na(indent_i)) {
return(NULL)
}
as.integer(indent_i)
}