-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathutils_render_grid.R
More file actions
1651 lines (1279 loc) · 42.5 KB
/
utils_render_grid.R
File metadata and controls
1651 lines (1279 loc) · 42.5 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#------------------------------------------------------------------------------#
#
# /$$
# | $$
# /$$$$$$ /$$$$$$
# /$$__ $$|_ $$_/
# | $$ \ $$ | $$
# | $$ | $$ | $$ /$$
# | $$$$$$$ | $$$$/
# \____ $$ \___/
# /$$ \ $$
# | $$$$$$/
# \______/
#
# This file is part of the 'rstudio/gt' project.
#
# Copyright (c) 2018-2025 gt authors
#
# For full copyright and license information, please look at
# https://gt.rstudio.com/LICENSE.html
#
#------------------------------------------------------------------------------#
# Layout ------------------------------------------------------------------
# For grid, the `create_*_component_g` functions are used to get a long-format
# description of every cell in the table.
# The goal of these layout functions is to get a position layout of cells as a
# data.frame, along with their class and style information. The layout should
# contain the following columns:
#
# left <integer>:
# Left position of a cell counting such that left = 1 is the first column in the
# table.
#
# right <integer>:
# Right position of cell equal to or larger than the 'left' column. When
# 'right' > 'left', this corresponds to having an html 'colspan' attribute
# larger than 1.
#
# top <integer>:
# Top position of a cell counting such that top = 1 is the first row in the
# component. Note that that this applies to 'within-component' rows, and not
# the final table row. Final 'top' and 'bottom' values are assigned in the
# `combine_components()` function.
#
# bottom <integer>:
# Bottom position of a cell equal to or larger than the 'top' column. When
# 'bottom' > 'top', this corresponds to having an html 'rowspan' attribute
# larger than 1.
#
# label <character>:
# A text label to display in the cell. Note that while the label can contain
# html, it will not be rendered with markup unless the text grob function
# supports it, for example when using gridtext as follows:
# `as_gtable(..., text_grob = gridtext::richtext_grob)`
#
# classes <list of <character>>:
# One or multiple class names equivalent to the html class attributes that will
# determine which css classes are used to populate graphical parameters for
# the cell (font color, borders, padding etc.).
#
# style <character>:
# A style string as computed by `add_css_styles()` or `NA` when a cell has
# no set styling options
#
# name <character>:
# A base name to assign in the gtable. This makes it easier to use
# `gtable::gtable_filter()` to select some types of cells.
create_caption_component_g <- function(data) {
table_caption <- dt_options_get_value(data = data, option = "table_caption")
if (all(is.na(table_caption))) {
return(NULL)
}
n_cols_total <- get_effective_number_of_columns(data = data)
grid_layout(
left = 1,
right = n_cols_total,
top = 1,
label = table_caption,
classes = list(c("gt_caption", "gt_center")),
style = NA_character_,
name = "caption"
)
}
create_heading_component_g <- function(data) {
if (!dt_heading_has_title(data = data)) {
return(NULL)
}
heading <- dt_heading_get(data = data)
heading[lengths(heading) == 0] <- list("")
styles_tbl <- dt_styles_get(data = data)
subtitle_defined <- dt_heading_has_subtitle(data = data)
footnotes_tbl <- dt_footnotes_get(data = data)
footnote_title_marks <- NULL
title_styles <- NA_character_
if ("title" %in% styles_tbl$locname) {
title_styles <- vctrs::vec_slice(styles_tbl$html_style, styles_tbl$locname == "title")
if (length(title_styles) == 0) {
title_styles <- NA_character_
}
}
subtitle_styles <- NA_character_
if (subtitle_defined && "subtitle" %in% styles_tbl$locname) {
subtitle_styles <- vctrs::vec_slice(styles_tbl$html_style, styles_tbl$locname == "subtitle")
if (length(subtitle_styles) == 0) {
subtitle_styles <- NA_character_
}
}
# The footnote marks are not great, but might look OK with {gridtext}
footnote_title_marks <- ""
if ("title" %in% footnotes_tbl$locname) {
footnote_title_marks <-
coalesce_marks(
fn_tbl = footnotes_tbl,
locname = "title"
)
footnote_title_marks <-
footnote_mark_to_html(
data = data,
mark = footnote_title_marks
)
}
footnote_subtitle_marks <- ""
if (subtitle_defined && "subtitle" %in% footnotes_tbl$locname) {
footnote_subtitle_marks <-
coalesce_marks(
fn_tbl = footnotes_tbl,
locname = "subtitle"
)
footnote_subtitle_marks <-
footnote_mark_to_html(
data = data,
mark = footnote_subtitle_marks
)
}
title_text <- paste0(heading$title, footnote_title_marks)
subtitle_text <- paste0(heading$subtitle, footnote_subtitle_marks)
title_classes <- c("gt_heading", "gt_title", "gt_font_normal")
subtitle_classes <- sub("title", "subtitle", title_classes, fixed = TRUE)
if (subtitle_defined) {
subtitle_classes <- c("gt_bottom_border", subtitle_classes)
} else {
title_classes <- c("gt_bottom_border", title_classes)
}
n_cols_total <- get_effective_number_of_columns(data = data)
out <-
grid_layout(
left = 1,
right = n_cols_total,
top = c(1, 2),
label = c(title_text, subtitle_text),
classes = list(title_classes, subtitle_classes),
style = c(title_styles, subtitle_styles),
name = c("title", "subtitle")
)
vctrs::vec_slice(out, nzchar(out$label))
}
create_columns_component_g <- function(data) {
column_labels_hidden <-
dt_options_get_value(
data = data,
option = "column_labels_hidden"
)
if (column_labels_hidden) {
return(NULL)
}
stubh <- dt_stubhead_get(data = data)
styles_tbl <- dt_styles_get(data = data)
body <- dt_body_get(data = data)
stub_layout <- get_stub_layout(data = data)
spanner_row_count <-
dt_spanners_matrix_height(
data = data,
omit_columns_row = TRUE
)
cols_alignment <- dt_boxhead_get_vars_align_default(data = data)
rtl_detect <-
vapply(
body[dt_boxhead_get_vars_default(data = data)],
FUN.VALUE = logical(1),
USE.NAMES = FALSE,
FUN = function(x) {
any(grepl(rtl_modern_unicode_charset, x))
}
)
to_right <- rtl_detect & cols_alignment != "center"
cols_alignment[to_right] <- "right"
headings_vars <- dt_boxhead_get_vars_default(data = data)
headings_labels <- dt_boxhead_get_vars_labels_default(data = data)
stubhead_style_attrs <- subset(styles_tbl, locname == "stubhead")
spanner_style_attrs <- subset(styles_tbl, locname == "columns_groups")
column_style_attrs <- subset(styles_tbl, locname == "columns_columns")
# Layout stubhead
stubhead_cell <- NULL
if (length(stub_layout) > 0) {
stub_style <- NA_character_
if (nrow(stubhead_style_attrs) > 0) {
stub_style <- stubhead_style_attrs$html_style %||% NA_character_
}
stubhead_cell <-
grid_layout(
left = 1, right = length(stub_layout),
bottom = spanner_row_count + 1, top = 1,
label = stubh$label,
classes = list(c("gt_col_heading", "gt_columns_bottom_border", "gt_left")),
style = stub_style,
name = "stubhead"
)
}
spanner_cells <- NULL
if (spanner_row_count > 0) {
# We transpose spanners to easily compute RLE
spanners <-
t(dt_spanners_print_matrix(
data = data,
include_hidden = FALSE
))
spanner_ids <-
t(dt_spanners_print_matrix(
data = data,
include_hidden = FALSE,
ids = TRUE
))
index <- match(spanner_ids, spanner_ids)
# recall spanners is transposed so taking `col(spanners)` gives us the row
rle <-
vctrs::vec_unrep(
vctrs::data_frame(
spanner = index,
row = as.vector(col(spanners))
)
)
# Annotate runs
rle$end <- stats::ave(rle$times, rle$key$row, FUN = cumsum)
rle$start <- rle$end - rle$times + 1
rle$id <- spanner_ids[rle$key$spanner]
# Filter out NA runs and non-spanner columns
rle <- vctrs::vec_slice(rle, !is.na(rle$id))
rle <- vctrs::vec_slice(rle, rle$key$row != spanner_row_count + 1)
spanner_style <- rep(NA_character_, nrow(rle))
if (nrow(spanner_style_attrs) > 0) {
i <- match(rle$id, spanner_style_attrs$grpname)
spanner_style[i] <- spanner_style_attrs$html_style
}
classes <- rep(list(c("gt_center", "gt_column_spanner")), nrow(rle))
at_top <- rle$key$row == 1
classes[at_top] <- lapply(
classes[at_top], c,
c("gt_columns_top_border", "gt_columns_spanner_outer")
)
spanner_cells <-
grid_layout(
left = rle$start + length(stub_layout),
right = rle$end + length(stub_layout),
top = rle$key$row,
label = spanners[rle$key$spanner],
classes = classes,
style = spanner_style,
name = "spanner"
)
}
column_style <- rep(NA_character_, length(headings_vars))
column_style[column_style_attrs[["colnum"]]] <-
column_style_attrs[["html_style"]]
cols_alignment <- paste0("gt_", cols_alignment)
classes <- lapply(cols_alignment, function(x) {
c("gt_col_heading", "gt_columns_bottom_border", x)
})
column_cells <-
grid_layout(
left = seq_along(headings_vars) + length(stub_layout),
top = spanner_row_count + 1,
label = headings_labels,
classes = classes,
style = column_style,
name = "column_label"
)
vctrs::vec_c(stubhead_cell, spanner_cells, column_cells)
}
create_body_component_g <- function(data) {
groups_rows_df <- dt_groups_rows_get(data = data)
# Create group headings
group_headings <- group_headings_g(data = data)
# Compute summary tables
grand_summary_side <-
summary_row_side(data = data, group_id = grand_summary_col)
grand_summaries <-
summary_rows_g(
data = data,
group_id = grand_summary_col,
side_grand_summary = grand_summary_side
)
group_summaries <-
summary_rows_g(data = data, group_id = groups_rows_df$group_id)
rows <- body_cells_g(data = data)
# Weave summaries, headings and body
row_start <- groups_rows_df$row_start
row_end <- groups_rows_df$row_end
if (!is.null(group_headings)) {
rows <- insert_before(rows, row_start, group_headings)
row_start <- row_start + cumsum(lengths(group_headings))
row_end <- row_end + cumsum(lengths(group_headings))
}
summary_side <- groups_rows_df$summary_row_side
summary_side <- summary_side[groups_rows_df$has_summary_rows][1]
if (identical(summary_side, "top")) {
rows <- insert_before(rows, row_start, group_summaries)
row_start <- row_start + cumsum(lengths(group_summaries))
row_end <- row_end + cumsum(lengths(group_summaries))
}
if (identical(summary_side, "bottom")) {
rows <- insert_after(rows, row_end, group_summaries)
row_start <- row_start + seq_along(row_start) - 1L
row_end <- row_end + seq_along(row_end)
}
if (sum(vctrs::list_sizes(grand_summaries %||% list())) > 0) {
if (grand_summary_side == "top") {
rows <- c(grand_summaries[[1]], rows)
} else {
rows <- c(rows, grand_summaries[[1]])
}
}
if (identical(rows, list())) {
return(NULL)
}
sizes <- vctrs::list_sizes(rows)
row_num <- rep(cumsum(sizes > 0), sizes)
rows <- vctrs::vec_c(!!!rows)
rows$top <- rows$top + row_num
rows$bottom <- rows$bottom + row_num
rows$classes <- lapply(rows$classes, c, list("gt_row"))
rows
}
group_headings_g <- function(data = data) {
stub <- get_stub_layout(data = data)
if ("group_label" %in% stub) {
return(NULL)
}
styles_tbl <- dt_styles_get(data = data)
groups_rows_df <- dt_groups_rows_get(data = data)
n_groups <- nrow(groups_rows_df)
n_cols_total <- get_effective_number_of_columns(data = data)
lapply(
seq_len(n_groups),
function(i) {
group_id <- groups_rows_df[["group_id"]][[i]]
group_label <- groups_rows_df[["group_label"]][[i]]
row_style_row_groups_tbl <-
dt_styles_pluck(
styles_tbl = styles_tbl,
locname = "row_groups",
grpname = group_id
)
row_style_group_heading_row <- row_style_row_groups_tbl[["html_style"]]
if (length(row_style_group_heading_row) == 0) {
row_style_group_heading_row <- NA
}
group_class <-
if (is.na(group_label) || group_label == "") {
group_label <- ""
"gt_empty_group_heading"
} else {
"gt_group_heading"
}
list(grid_layout(
left = 1, right = n_cols_total,
top = 0,
label = group_label,
classes = list(c(group_class, "gt_group_heading_row")),
style = row_style_group_heading_row,
name = "group_heading"
))
}
)
}
body_cells_g <- function(data) {
summaries_present <- dt_summary_exists(data = data)
groups <- dt_groups_rows_get(data = data)
styles_tbl <- dt_styles_get(data = data)
cell_matrix <- get_body_component_cell_matrix(data = data)
cell_rows <- as.vector(row(cell_matrix))
cell_cols <- as.vector(col(cell_matrix))
row_seq <- seq_len(nrow(cell_matrix))
n_rows <- nrow(cell_matrix)
n_cols_total <- get_effective_number_of_columns(data = data)
stub_layout <- get_stub_layout(data = data)
has_two_col_stub <- "group_label" %in% stub_layout
odd_class <- even_class <- rep_len(list(NULL), n_cols_total)
# Set striping
striped_stub <-
dt_options_get_value(data = data, option = "row_striping_include_stub")
striped_body <-
dt_options_get_value(data = data, option = "row_striping_include_table_body")
if (striped_body) {
even_class <- rep(list("gt_striped"), n_cols_total)
}
# Set stub class
row_label_col <- which(stub_layout == "rowname")
if (length(row_label_col) > 0) {
odd_class[[row_label_col]] <- "gt_stub"
if (striped_stub) {
even_class[[row_label_col]] <- c("gt_stub", "gt_striped")
} else {
even_class[[row_label_col]] <- "gt_stub"
}
}
# Weave even/odd classes into matrix
cell_classes <- rep_len(list(odd_class, even_class), n_rows)
cell_classes <- inject(rbind(!!!cell_classes))
# Set column alignment
align_class <-
c(
dt_boxhead_get_alignments_in_stub(data = data),
dt_boxhead_get_vars_align_default(data = data)
)
align_class <- paste0("gt_", align_class)[cell_cols]
# Adjust alignment for RtL script
has_rtl <- grepl(rtl_modern_unicode_charset, cell_matrix)
align_class[has_rtl & align_class != "gt_center"] <- "gt_right"
# Set stub indentation
indent_class <- first_class <- matrix(list(NULL), n_rows, n_cols_total)
if (length(row_label_col) > 0) {
stub_indent <- dt_stub_df_get(data = data)
stub_indent <-
vctrs::vec_slice(stub_indent %||% list(), stub_indent$indent > 0)
if (NROW(stub_indent) > 0) {
i <- match(row_seq, stub_indent$rownum_i)
i <- i[!is.na(i)]
indent_class[i, row_label_col] <- paste0("gt_indent_", stub_indent$indent)
}
}
# Set first in group
first_group <- which(row_seq %in% groups$row_start)
first_class[first_group, ] <- "gt_row_group_first"
# Combine classes
classes <- Map(c, first_class, indent_class, cell_classes, align_class)
# Set style for data/stub cells
style <- matrix(NA_character_, n_rows, n_cols_total)
cell_style <-
dt_styles_pluck(
styles_tbl = styles_tbl,
locname = c("data", "stub")
)
style_col <- cell_style$colnum + length(stub_layout)
style[cbind(cell_style$rownum, style_col)] <- cell_style$html_style
# Set style for row group labels
if (has_two_col_stub) {
cell_style <-
dt_styles_pluck(
styles_tbl = styles_tbl,
locname = "row_groups"
)
style_row <- with(groups, row_start[match(cell_style$grpname, group_id)])
style[style_row, 1] <- cell_style$html_style
# Override group label
cell_matrix[first_group, 1] <- groups$group_label
}
# Set layout
layout <-
grid_layout(
left = cell_cols,
top = 0, # will be completed later
label = undim(cell_matrix),
classes = undim(classes),
style = undim(style),
name = "body_cell"
)
if (has_two_col_stub) {
# Set stub row group(s)
group_cell <- which(cell_rows %in% groups$row_start & cell_cols == 1)
group <- match(cell_rows[group_cell], groups$row_start)
extra_rows <- (groups$row_end - groups$row_start + 1)[group]
layout$bottom[group_cell] <- extra_rows
layout$classes[group_cell] <-
lapply(layout$classes[group_cell], c, "gt_stub_row_group")
# Shift upward if summaries on top
i <-
summaries_present &
(groups$has_summary_rows %||% FALSE) &
(groups$summary_row_side %||% "bottom" == "top") &
rep(TRUE, nrow(groups)) # ensures correct length if columns are missing
i[is.na(i)] <- FALSE
group_cell <- group_cell[i]
layout[group_cell, c("top", "bottom")] <-
layout[group_cell, c("top", "bottom")] - 1
# Delete empty cells
delete <- which(!(cell_rows %in% groups$row_start) & cell_cols == 1)
if (rlang::has_length(delete)) {
# Don't attempt delete doesn't have length
# will likely occur in the case of all groups of length 1 #1803
layout <- vctrs::vec_slice(layout, -delete)
cell_rows <- vctrs::vec_slice(cell_rows, -delete)
}
}
# Split by row
rows <- vector("list", n_rows)
layout <- vctrs::vec_split(layout, cell_rows)
rows[layout$key] <- layout$val
rows
}
summary_rows_g <- function(data, group_id, side_grand_summary = "bottom") {
if (is.null(group_id)) {
return(NULL)
}
groups_rows_df <- dt_groups_rows_get(data = data)
list_of_summaries <- dt_summary_df_get(data = data)
summaries_present <- dt_summary_exists(data = data)
styles_tbl <- dt_styles_get(data = data)
needs_summary <-
summaries_present &
(((groups_rows_df$has_summary_rows %||% FALSE) &
!is.na(groups_rows_df$summary_row_side %||% FALSE) &
((groups_rows_df$summary_row_side %||% "left") %in% c("top", "bottom"))) |
(group_id == grand_summary_col))
if (!any(needs_summary)) {
return(NULL)
}
groups_rows_df <- groups_rows_df[needs_summary, , drop = FALSE]
group_summary_vars <- dt_boxhead_get_vars_default(data = data)
summary_row_type <- rep("", nrow(groups_rows_df))
group_type <-
!is.na(group_id) &
group_id %in% names(list_of_summaries$summary_df_display_list) &
group_id != grand_summary_col
grand_type <-
!is.na(group_id) &
group_id == grand_summary_col
summary_row_type <- rep("", max(length(group_type), length(grand_type)))
summary_row_type[grand_type] <- "grand"
summary_row_type[group_type] <- "group"
summary_df <- list_of_summaries$summary_df_display_list[group_id]
n_rows <- vctrs::list_sizes(summary_df)
if (sum(n_rows) < 1) {
return(NULL)
}
summary_df <- vctrs::vec_c(!!!summary_df)
summary_df <-
dplyr::select(
summary_df,
dplyr::all_of(c(rowname_col_private, group_summary_vars)),
)
summary_row_type <- rep(summary_row_type, n_rows)
stub_layout <- get_stub_layout(data = data)
n_data_cols <- get_number_of_visible_data_columns(data = data)
ncol <- ncol(summary_df)
left <- right <- col(summary_df) + as.integer(length(stub_layout) > 1)
if (all(summary_row_type == "grand")) {
if (length(stub_layout) > 1) {
left[, 1] <- left[, 1] - 1
}
if (length(group_id) < length(summary_row_type)) {
group_id <- rep(group_id, length(summary_row_type))
}
}
extra_classes <- matrix(list(NULL), nrow = nrow(summary_df), ncol)
extra_classes[, 1] <- lapply(extra_classes[, 1], c, "gt_stub")
col_alignment <- c("left", dt_boxhead_get_vars_align_default(data = data))
alignment_classes <- paste0("gt_", col_alignment)
extra_classes[] <-
Map(
extra_classes,
alignment_classes[as.vector(col(extra_classes))],
f = c
)
styles <- matrix(NA, nrow = nrow(summary_df), ncol)
is_first <- is_last <- rep(FALSE, sum(n_rows))
is_first[c(1, cumsum(n_rows)[-length(n_rows)] + 1)] <- TRUE
is_last[cumsum(n_rows)] <- TRUE
inner_row <- unlist(lapply(n_rows, seq_len), use.names = FALSE)
for (i in seq_len(nrow(summary_df))) {
last_row_class <- "gt_last_summary_row"
if (summary_row_type[i] == "grand") {
styles_resolved_row <-
dt_styles_pluck(
styles_tbl = styles_tbl,
locname = "grand_summary_cells",
grpname = group_id[i],
rownum = i
)
summary_row_class <- "gt_grand_summary_row"
first_row_class <- "gt_first_grand_summary_row"
if (side_grand_summary == "top") {
first_row_class <- "gt_grand_summary_row"
last_row_class <- "gt_last_grand_summary_row_top"
}
} else {
styles_resolved_row <-
dt_styles_pluck(
styles_tbl = styles_tbl,
locname = "summary_cells",
grpname = group_id[i],
grprow = inner_row[i]
)
summary_row_class <- "gt_summary_row"
first_row_class <-
if ("rowname" %in% stub_layout) {
"gt_first_summary_row.thick"
} else {
"gt_first_summary_row"
}
}
row_styles <-
build_row_styles(
styles_resolved_row = styles_resolved_row,
include_stub = TRUE,
n_cols = n_data_cols
)
extra_class <- extra_classes[i, ]
extra_class <- lapply(extra_class, function(x) c(summary_row_class, x))
if (is_first[i]) {
extra_class <- lapply(extra_class, c, first_row_class)
}
if (is_last[i]) {
extra_class <- lapply(extra_class, c, last_row_class)
}
extra_classes[i, ] <- extra_class
styles[i, ] <- row_styles
}
if (all(summary_row_type == "grand")) {
name <- "grand_summary"
} else {
name <- "group_summary"
}
out <-
grid_layout(
left = undim(left),
right = undim(right),
top = 0,
label = unlist(summary_df, use.names = FALSE),
classes = undim(extra_classes),
style = undim(styles),
name = name
)
out <- vctrs::vec_split(out, as.vector(row(summary_df)))$val
vctrs::vec_chop(out, sizes = n_rows)
}
create_source_notes_component_g <- function(data) {
source_notes <- dt_source_notes_get(data = data)
if (is.null(source_notes)) {
return(NULL)
}
styles_tbl <- dt_styles_get(data = data)
n_cols_total <- get_effective_number_of_columns(data = data)
style <- NA
if ("source_notes" %in% styles_tbl$locname) {
source_notes_style <- vctrs::vec_slice(styles_tbl, styles_tbl$locname == "source_notes")
if (nrow(source_notes_style)) {
style <- source_notes_style$html_style
}
}
multiline <- dt_options_get_value(data = data, option = "source_notes_multiline")
if (multiline) {
separator <- "\n"
} else {
separator <- dt_options_get_value(data = data, option = "source_notes_sep")
separator <- gsub(" (?= )", "\u00A0", separator, perl = TRUE)
}
source_notes <- paste(source_notes, collapse = separator)
grid_layout(
left = 1,
right = n_cols_total,
top = 1,
label = source_notes,
classes = list(c("gt_sourcenotes", "gt_sourcenote")),
style = style,
name = "source_notes"
)
}
create_footnotes_component_g <- function(data) {
footnotes_tbl <- dt_footnotes_get(data)
if (nrow(footnotes_tbl) < 1) {
return(NULL)
}
styles_tbl <- dt_styles_get(data = data)
n_cols_total <- get_effective_number_of_columns(data = data)
footnotes_tbl <- dplyr::distinct(footnotes_tbl, fs_id, footnotes)
style <- NA
if ("footnotes" %in% styles_tbl$locname) {
footnotes_style <- vctrs::vec_slice(styles_tbl, styles_tbl$locname == "footnotes")
if (nrow(footnotes_style) > 0) {
style <- footnotes_style$html_style
}
}
multiline <- dt_options_get_value(data = data, option = "footnotes_multiline")
footnote_ids <- footnotes_tbl[["fs_id"]]
footnote_text <- footnotes_tbl[["footnotes"]]
marks <-
vapply(
footnote_ids,
FUN = footnote_mark_to_grid,
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
data = data,
location = "ftr"
)
marks[nzchar(marks)] <- paste0(marks[nzchar(marks)], " ")
text <-
vapply(
footnote_text,
FUN = process_text,
FUN.VALUE = character(1L),
USE.NAMES = FALSE,
context = "grid"
)
text <- paste0(marks, text)
if (multiline) {
separator <- "\n"
} else {
separator <- dt_options_get_value(data = data, option = "footnotes_sep")
separator <- gsub(" (?= )", "\u00A0", separator, perl = TRUE)
}
text <- paste(text, collapse = separator)
grid_layout(
left = 1,
right = n_cols_total,
top = 1,
label = text,
classes = list(c("gt_footnotes", "gt_footnote")),
style = style,
name = "footnotes"
)
}
#' Transform a footnote mark to a grid representation
#'
#' @noRd
footnote_mark_to_grid <- function(
data,
mark,
location = c("ref", "ftr")
) {
location <- match.arg(location)
if (is.na(mark)) {
return("")
}
spec <- get_footnote_spec_by_location(data = data, location = location)
if (is.null(spec)) {
spec <- "^i"
}
# Generate the CSS classes needed on the basis of whether the
# mark is one or more asterisk characters or anything else
if (!grepl("^[\\*]+?$", mark)) {
sup_class <- "gt_footnote_marks"
} else {
sup_class <- "gt_footnote_marks gt_asterisk"
}
is_sup <- grepl("^", spec, fixed = TRUE)
if (grepl(".", spec, fixed = TRUE)) mark <- paste0(mark, ".")
if (grepl("(", spec, fixed = TRUE)) mark <- paste0("(", mark)
if (grepl("[", spec, fixed = TRUE)) mark <- paste0("[", mark)
if (grepl(")", spec, fixed = TRUE)) mark <- paste0(mark, ")")
if (grepl("]", spec, fixed = TRUE)) mark <- paste0(mark, "]")
# Not supported in grid
if (grepl("i", spec, fixed = TRUE)) {
font_style <- "italic"
} else {
font_style <- "normal"
}
if (grepl("b", spec, fixed = TRUE)) {
font_weight <- "bold"
} else {
font_weight <- "normal"
}
# return mark as plain text (all styling is ignored)
mark
}
# Cells -------------------------------------------------------------------
# This is the main function that takes a label and a style and returns
# a graphical object for display in a gtable.
# Note that 'style' is not an html style attribute but a combination of class
# and style attributes parsed by `grid_resolve_style()`,
render_grid_cell <- function(
label,