-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathrender_as_i_html.R
More file actions
1102 lines (967 loc) · 36.7 KB
/
render_as_i_html.R
File metadata and controls
1102 lines (967 loc) · 36.7 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
#
#------------------------------------------------------------------------------#
#' Transform a **gt** table object to an HTML table
#'
#' Take a `gti_tbl` table object and transform it to an HTML table.
#'
#' @param data A table object that is created using the `gt()` function.
#'
#' @return A character object with an HTML table.
#'
#' @noRd
render_as_ihtml <- function(data, id) {
# Get the built data for the table body in the HTML context
data <- build_data(data = data, context = "html")
# Upgrade the `_styles` component to give it a `html_style` column
# with CSS style rules
data <- add_css_styles(data = data)
# Get any available source notes or footnotes in the table
source_notes <- dt_source_notes_get(data = data)
footnotes <- dt_footnotes_get(data = data)
# Determine if the rendered table should have a footer section
has_footer_section <- !is.null(source_notes) || nrow(footnotes) >= 1
# Determine if the rendered table should have a header section
has_header_section <- dt_heading_has_title(data = data)
# Determine if there are tab spanners
has_tab_spanners <- dt_spanners_exists(data = data)
# Obtain the language from the `locale`, if provided
locale <- normalize_locale(dt_locale_get_value(data = data))
# Generate a `lang_defs` object to pass to the `language` argument
lang_defs <- get_ihtml_translations(locale)
column_groups <- dt_boxhead_get_vars_groups(data = data)
# if dt_boxhead_get_vars_groups is fixed, this will no longer be necessary.
if (identical(column_groups, NA_character_)) {
column_groups <- NULL
}
# Derive styling for the stubhead
stubhead_style <- dt_styles_get(data)
if (!is.null(stubhead_style)) {
stubhead_style <- stubhead_style[stubhead_style$locname == "stubhead"]
if (nrow(stubhead_style) == 0) {
stubhead_style <- NULL
} else {
stubhead_style <- stubhead_style$html_style
}
}
rownames_to_stub <- stub_rownames_has_column(data)
# value to use for rowname_col or groupname_col title
# Will use it for rowname_col only if groupname_col is undefined.
# Will use it only for the first groupname_col.
stub_label <- dt_stubhead_get(data)$label
if (is.null(stub_label)) {
# No label if stubhead label is undefined.
rowname_label <- ""
groupname_label <- ""
} else if (!is.null(column_groups)) {
rowname_label <- ""
groupname_label <- stub_label
} else {
rowname_label <- stub_label
groupname_label <- NULL
}
# Apply the stubhead styling to row group heading
if (is.null(column_groups)) {
rowname_header_style <- stubhead_style
rwo_group_header_style <- NULL
} else {
# Since row names don't appear under the row group column, style it (even if it is different in non-intereactive)
rowname_header_style <- stubhead_style
row_group_header_style <- stubhead_style
}
# Obtain the underlying data table (including group rows)
data_tbl0 <- dt_data_get(data = data)
# Only preserve columns that are not hidden (group cols will be added later)
data_tbl_vars <- dt_boxhead_get_vars_default(data = data)
data_tbl <- data_tbl0[, data_tbl_vars, drop = FALSE]
#nocov start
if (ncol(data_tbl) < 1) {
# Stop function if there are no visible columns
cli::cli_abort(c(
"When displaying an interactive gt table, there must be at least one visible column.",
"*" = "Check that the input data table has at least one column,",
"*" = "Failing that, look at whether all columns have been inadvertently hidden."
))
}
#nocov end
# use of special .rownames doesn't work.
# Workaround https://github.com/glin/reactable/issues/378
# rstudio/gt#1702
if (rownames_to_stub) {
rowname_col <- dt_boxhead_get_var_stub(data)
if (length(rowname_col) == 1) {
# avoid base R error when setting duplicate row names.
row_names <- as.character(data$`_data`[[rowname_col]])
# Convert to NA string to avoid wrong output.
# TODO figure out if there is a way to get the sub_missing value.
# With data$`_substitutions`
row_names <- dplyr::coalesce(row_names, "NA")
attr(data_tbl, "row.names") <- row_names
} else {
rownames_to_stub <- FALSE
}
}
# Obtain column label attributes
column_names <- dt_boxhead_get_vars_default(data = data)
column_labels <- dt_boxhead_get_vars_labels_default(data = data)
column_alignments <- dt_boxhead_get_vars_align_default(data = data)
# Obtain widths for each visible column label
boxh <- dt_boxhead_get(data = data)
column_widths <- boxh[boxh$type %in% c("default", "stub"), , drop = FALSE]
# ensure that stub is first
column_widths <- column_widths[order(column_widths$type, decreasing = TRUE), "column_width", drop = TRUE]
column_widths <- unlist(column_widths)
# Transform any column widths to integer values
if (!is.null(column_widths)) {
column_widths <-
vapply(
column_widths,
FUN.VALUE = integer(1L),
USE.NAMES = FALSE,
FUN = function(x) {
if (grepl("px", x, fixed = TRUE)) {
x <- as.integer(gsub("px", "", x, fixed = TRUE))
} else {
x <- NA_integer_
}
x
}
)
}
# Get all options settable in `tab_options()`
tbl_opts <- dt_options_get_values(data)
# Get specific options values
height <- tbl_opts$ihtml_height
use_pagination <- tbl_opts$ihtml_use_pagination
use_pagination_info <- tbl_opts$ihtml_use_pagination_info
use_search <- tbl_opts$ihtml_use_search
use_sorting <- tbl_opts$ihtml_use_sorting
use_filters <- tbl_opts$ihtml_use_filters
use_resizers <- tbl_opts$ihtml_use_resizers
use_highlight <- tbl_opts$ihtml_use_highlight
use_compact_mode <- tbl_opts$ihtml_use_compact_mode
use_text_wrapping <- tbl_opts$ihtml_use_text_wrapping
use_page_size_select <- tbl_opts$ihtml_use_page_size_select
page_size_default <- tbl_opts$ihtml_page_size_default
page_size_values <- tbl_opts$ihtml_page_size_values
pagination_type <- tbl_opts$ihtml_pagination_type
selection_mode <- tbl_opts$ihtml_selection_mode
if (is.na(selection_mode)) selection_mode <- NULL
onClick <- if (!is.null(selection_mode)) "select"
use_row_striping <- tbl_opts$row_striping_include_table_body
row_striping_color <- tbl_opts$row_striping_background_color
table_width <- tbl_opts$table_width
table_background_color <- tbl_opts$table_background_color
table_font_size <- tbl_opts$table_font_size
table_font_names <- tbl_opts$table_font_names
table_font_color <- tbl_opts$table_font_color
table_border_right_style <- tbl_opts$table_border_right_style
table_border_right_color <- tbl_opts$table_border_right_color
table_border_left_style <- tbl_opts$table_border_left_style
table_border_left_color <- tbl_opts$table_border_left_color
table_border_top_color <- tbl_opts$table_border_top_color
heading_border_bottom_color <- tbl_opts$heading_border_bottom_color
column_labels_border_top_style <- tbl_opts$column_labels_border_top_style
column_labels_border_top_width <- tbl_opts$column_labels_border_top_width
column_labels_border_top_color <- tbl_opts$column_labels_border_top_color
column_labels_border_bottom_style <- tbl_opts$column_labels_border_bottom_style
column_labels_border_bottom_width <- tbl_opts$column_labels_border_bottom_width
column_labels_border_bottom_color <- tbl_opts$column_labels_border_bottom_color
# Don't allow NA
column_labels_background_color <- tbl_opts$column_labels_background_color
# Apply stub font weight to
stub_font_weight <- tbl_opts$stub_font_weight
# Don't allow NA
column_labels_background_color <- tbl_opts$column_labels_background_color
if (is.na(column_labels_background_color)) {
# apply all column labels formatting to both heading + groupCol styling (nothing specific for spanners styling in gt?)
column_labels_background_color <- "transparent"
}
column_labels_font_weight <- tbl_opts$column_labels_font_weight
# Apply stub font weight to
stub_font_weight <- tbl_opts$stub_font_weight
# Apply font weight to groupname_col title
row_group_font_weight <- tbl_opts$row_group_font_weight
row_group_background_color <- tbl_opts$row_group_background_color
table_body_font_weight <- tbl_opts$table_font_weight
table_body_hlines_style <- tbl_opts$table_body_hlines_style
table_body_hlines_color <- tbl_opts$table_body_hlines_color
table_body_hlines_width <- tbl_opts$table_body_hlines_width
table_body_vlines_style <- tbl_opts$table_body_vlines_style
table_body_vlines_color <- tbl_opts$table_body_vlines_color
table_body_vlines_width <- tbl_opts$table_body_vlines_width
horizontal_borders <- tbl_opts$table_body_hlines_style
veritcal_borders <- tbl_opts$table_body_vlines_style
borderless_borders <- horizontal_borders == "none" && veritcal_borders == "none"
all_borders <- horizontal_borders != "none" && veritcal_borders != "none"
# for row names + summary label
stub_border_color <- tbl_opts$stub_border_color
stub_border_style <- tbl_opts$stub_border_style
# Apply stub font weight to
stub_font_weight <- tbl_opts$stub_font_weight
stub_background_color <- tbl_opts$stub_background_color
borderless_borders <- tbl_opts$table_body_hlines_style == "none"
column_labels_font_weight <- tbl_opts$column_labels_font_weight
# Apply font weight to groupname_col title
row_group_font_weight <- tbl_opts$row_group_font_weight
table_body_font_weight <- tbl_opts$table_font_weight
# for row names + summary label
stub_font_weight <- tbl_opts$stub_font_weight
emoji_symbol_fonts <-
c(
"Apple Color Emoji", "Segoe UI Emoji",
"Segoe UI Symbol", "Noto Color Emoji"
)
table_font_names <- base::setdiff(table_font_names, emoji_symbol_fonts)
font_family_str <-
as_css_font_family_attr(
font_vec = table_font_names,
value_only = TRUE
)
if (table_width == "auto") table_width <- NULL
if (rownames_to_stub) {
# Create colDef row name with special ".rownames" from reactable.
row_name_col_def <- list(reactable::colDef(
name = rowname_label,
# make sure the cells_stubhead() footnote renders properly.
html = TRUE,
style = list(
fontWeight = stub_font_weight,
color = if (!is.na(stub_background_color)) unname(ideal_fgnd_color(stub_background_color)) else NULL,
borderRight = stub_border_color,
borderRightStyle = stub_border_style,
backgroundColor = stub_background_color#,
# borderLeft, borderRight are possible
),
# part of the stubhead
headerStyle = rowname_header_style
# TODO pass on other attributes of row names column if necessary.
))
names(row_name_col_def) <- ".rownames"
} else {
row_name_col_def <- NULL
}
#
# Determine which columns will undergo some formatting
#
formats <- dt_formats_get(data = data)
# Get a list of vectors that include columns taking part
# in some formatting operation
formatted_columns <-
lapply(
formats,
FUN = function(x) {
columns <- x$cols
rows <- x$rows
compat <- x$compat
compat_columns <- c()
for (col in columns) {
if (
col %in% colnames(data_tbl) &&
is_compatible_formatter(
table = data_tbl,
column = col,
rows = rows,
compat = compat
)
) {
compat_columns <- c(compat_columns, col)
}
}
compat_columns
}
)
# Flatten this list of vectors to a single vector of unique column names
formatted_columns <- unique(flatten_list(formatted_columns))
# Format col_merge cols #1785
col_merge_cols <- dt_col_merge_get_vars(data)
formatted_columns <- c(formatted_columns, col_merge_cols)
# format substitutions #1759
substitution <- dt_substitutions_get(data)
if (length(substitution) > 0) {
sub_cols <- substitution[[1]]$cols
formatted_columns <- c(formatted_columns, sub_cols)
}
# take unique values
formatted_columns <- unique(formatted_columns)
# Create a list of column definitions
col_defs <-
lapply(
seq_along(column_names),
FUN = function(x) {
# Only perform extraction of formatted cells if there is an
# indication that formatting will be performed on a column`
# or if it is the result of column merge.
if (column_names[x] %in% formatted_columns) {
formatted_cells <-
extract_cells(
data = data,
columns = column_names[x],
output = "html"
)
cell_fn <- function(value, index) formatted_cells[index]
} else {
cell_fn <- NULL
}
reactable::colDef(
cell = cell_fn,
name = column_labels[x],
align = column_alignments[x],
# Has no effect with sub_missing
na = "NA",
# TODO support `summary_rows()` via `aggregate` #1359
# TODO support `grand_summary_rows()` via `footer`. #1359
width = if (is.null(column_widths) || is.na(column_widths[x])) NULL else column_widths[x],
html = TRUE
)
}
)
names(col_defs) <- column_names
# Customize groupname_col and add to data_tbl
if (!is.null(column_groups)) {
# FIXME how should row_groups_as_column behave?
# FIXME find a way to remove borders to act like it is the value for the group
# should it just be a normal row at the left?
group_col_defs <- list()
# Hack from glin/reactable#94 to hide the number of observations
grp_fn <- reactable::JS("
function(cellInfo) {
return cellInfo.value
}")
for (i in seq_along(column_groups)) {
if (i == 1) {
# Use the stubhead label for the first group
group_label <- groupname_label
row_group_header_style <- stubhead_style
} else {
# by default, don't name groupname_col for consistency with non-interactive
group_label <- ""
row_group_header_style <- stubhead_style
}
group_col_defs[[i]] <-
reactable::colDef(
name = group_label,
# make sure the cells_stubhead() footnote renders properly.
html = TRUE,
style = list(
`font-weight` = row_group_font_weight,
color = if (is.na(row_group_background_color)) NULL else unname(ideal_fgnd_color(row_group_background_colorfgggee )),
backgroundColor = row_group_background_color,
borderStyle = "none",
borderColor = "transparent",
borderTopColor = "transparent",
borderBottomColor = "gray38"
),
headerStyle = row_group_header_style,
# The total number of rows is wrong in colGroup, possibly due to the JS fn
grouped = grp_fn,
# FIXME Should groups be sticky? (or provide a way to do this)
sticky = NULL
)
}
names(group_col_defs) <- column_groups
groupname_col <- column_groups
# for defaultExpanded = TRUE
expand_groupname_col <- TRUE
# modify data_tbl to include
data_tbl <- vctrs::vec_cbind(
data_tbl,
data_tbl0[ , groupname_col, drop = FALSE]
)
} else {
groupname_col <- NULL
group_col_defs <- NULL
expand_groupname_col <- FALSE
}
#
# Generate custom styles for `defaultColDef`
#
# Add group colDef and rowname colDef to general col_def
col_defs <- c(col_defs, group_col_defs, row_name_col_def)
styles_tbl <- dt_styles_get(data = data)
body_styles_tbl <- vctrs::vec_slice(styles_tbl, styles_tbl$locname %in% c("data", "stub"))
body_styles_tbl <- dplyr::arrange(body_styles_tbl, colnum, rownum)
body_styles_tbl <- body_styles_tbl[c("colname", "rownum", "html_style")]
# Generate some options for global body style
# They will end up being added inside the JS() function,
# So, they need to have this format.
global_body_style <-
paste0(
"borderLeftColor: '", tbl_opts$table_body_vlines_color, "', ",
"borderLeftStyle: '", tbl_opts$table_body_vlines_style, "', ",
"borderLeftWidth: '", tbl_opts$table_body_vlines_width, "', ",
"borderRightColor: '", tbl_opts$table_body_vlines_color, "', ",
"borderRightStyle: '", tbl_opts$table_body_vlines_style, "', ",
"borderRightWidth: '", tbl_opts$table_body_vlines_width, "', ",
"borderTopColor: '", tbl_opts$table_body_hlines_color, "', ",
"borderTopStyle: '", tbl_opts$table_body_hlines_style, "', ",
"borderTopWidth: '", tbl_opts$table_body_hlines_width, "' "
)
# Generate styling rule per combination of `colname` and
# `rownum` in `body_styles_tbl`
body_style_rules <-
vapply(
seq_len(nrow(body_styles_tbl)), FUN.VALUE = character(1L), USE.NAMES = FALSE,
FUN = function(x) {
colname <- body_styles_tbl[x, ][["colname"]]
rownum <- body_styles_tbl[x, ][["rownum"]]
html_style <- body_styles_tbl[x, ][["html_style"]]
html_style <- unlist(strsplit(html_style, "; ", fixed = TRUE))
html_style <- gsub("(-)\\s*(.)", "\\U\\2", html_style, perl = TRUE)
html_style <- gsub("(:)\\s*(.*)", ": '\\2'", html_style, perl = TRUE)
html_style <- paste(html_style, collapse = ", ")
html_style <- gsub(";'$", "'", html_style)
# Add the global body style afterwards. (Specific styling will have precedence)
html_style <- paste0(html_style, ", ", global_body_style)
paste0(
"if (colInfo.id === '", colname, "' & rowIndex === ", rownum, ") {\n",
" return { ", html_style , " }\n",
"}\n",
"return { ", global_body_style, "}",
"\n"
)
}
)
body_style_rules <- paste(body_style_rules, collapse = "")
body_style_js_str <-
paste0(
"function(rowInfo, colInfo) {\n",
"const rowIndex = rowInfo.index + 1\n",
body_style_rules,
"}",
collapse = ""
)
# TODO if `sub_missing()` is enabled globally, just use `na = ` here!
default_col_def <-
reactable::colDef(
style = reactable::JS(body_style_js_str),
# style = list(
# borderLeftStyle = tbl_opts$table_body_vlines_style,
# borderLeftColor = tbl_opts$table_body_vlines_color,
# borderLeftWidth = tbl_opts$table_body_vlines_width
#
# ),
minWidth = 125,
# Has no effect with sub_missing()
na = "NA",
width = NULL
)
# Generate the table header if there are any heading components
if (has_header_section) {
# These don't work in non-interactive context.
heading_title_font_weight <- tbl_opts$heading_title_font_weight
heading_subtitle_font_weight <- tbl_opts$heading_subtitle_font_weight
heading_background_color <- tbl_opts$heading_background_color
tbl_heading <- dt_heading_get(data = data)
heading_component <-
htmltools::div(
style = htmltools::css(
`font-family` = font_family_str,
`background-color` = heading_background_color,
`border-top-style` = "solid",
`border-top-width` = "2px",
`border-top-color` = "#D3D3D3",
`border-bottom-color` = "#D3D3D3",
`padding-bottom` = if (use_search) "8px" else NULL
),
htmltools::div(
class = "gt_heading gt_title gt_font_normal",
style = htmltools::css(
`text-size` = "bigger",
`font-weight` = heading_title_font_weight
),
htmltools::HTML(tbl_heading$title)
),
htmltools::div(
class = paste(
"gt_heading", "gt_subtitle",
if (use_search) "gt_bottom_border" else NULL
),
style = htmltools::css(
`font-weight` = heading_subtitle_font_weight,
`border-bottom-color` = "#D3D3D3"
),
htmltools::HTML(tbl_heading$subtitle)
)
)
} else {
heading_component <- NULL
}
# Generate the table footer if there are any footer components
if (has_footer_section) {
if (!is.null(source_notes)) {
source_notes_component <- create_source_notes_component_ihtml(data = data)
} else {
source_notes_component <- NULL
}
if (!is.null(footnotes)) {
footnotes_component <- create_footnotes_component_ihtml(data = data)
} else {
footnotes_component <- NULL
}
table_border_bottom_style <- tbl_opts$table_border_bottom_style
footer_component <-
htmltools::div(
style = htmltools::css(
`font-family` = font_family_str,
`border-top-style` = "solid",
`border-top-width` = "2px",
`border-top-color` = "#D3D3D3",
`border-bottom-style` = table_border_bottom_style,
`border-bottom-width` = "2px",
`border-bottom-color` = "#D3D3D3",
`padding-top` = "6px",
`padding-bottom` = "6px",
`padding-left` = "10px",
`padding-right` = "10px"
),
htmltools::div(source_notes_component),
htmltools::div(footnotes_component)
)
} else {
footer_component <- NULL
}
# Generate the column groups, if there are any tab_spanners
colgroups_def <- NULL
if (has_tab_spanners) {
hidden_columns <- dt_boxhead_get_var_by_type(data = data, type = "hidden")
spanners_df <- dt_spanners_get(data = data)
col_groups <- vctrs::vec_slice(
spanners_df, spanners_df$spanner_level == 1
)
for (i in seq_len(nrow(col_groups))) {
columns_group_i <- unlist(col_groups[i, ][["vars"]])
columns_group_i_diff <- base::setdiff(columns_group_i, hidden_columns)
col_groups[i, ][["vars"]][[1]] <- columns_group_i_diff
}
if (max(dt_spanners_get(data = data)$spanner_level) > 1) {
first_colgroups <- base::paste0(col_groups$built, collapse = "|")
cli::cli_warn(c(
"Interactive tables support a single spanner level.",
"*" = "Currently there are {max(dt_spanners_get(data = data)$spanner_level)} levels of tab spanners.",
"i" = "Only the first level will be used for the interactive table, that is: [{first_colgroups}]"
))
}
colgroups_def <-
apply(
col_groups, 1,
FUN = function(x) {
reactable::colGroup(
name = x$spanner_label,
columns = x$vars,
header = x$built,
html = TRUE,
align = NULL,
headerVAlign = NULL,
# TODO #194
sticky = NULL,
headerClass = NULL,
headerStyle = list(
fontWeight = "normal",
color = if (is.na(column_labels_background_color)) NULL else unname(ideal_fgnd_color(column_labels_background_color)),
backgroundColor = column_labels_background_color,
borderBottomStyle = column_labels_border_bottom_style,
borderBottomWidth = column_labels_border_bottom_width,
borderBottomColor = column_labels_border_bottom_color,
borderLeftStyle = column_labels_border_bottom_style,
borderLeftWidth = "4px",
borderLeftColor = "transparent",
borderRightStyle = column_labels_border_bottom_style,
borderRightWidth = "4px",
borderRightColor = "transparent"
)
)
}
)
}
# Generate the default theme for the table
tbl_theme <-
reactable::reactableTheme(
color = table_font_color,
backgroundColor = table_background_color,
borderColor = NULL,
borderWidth = NULL,
stripedColor = row_striping_color,
highlightColor = NULL,
cellPadding = NULL,
style = list(
`font-family` = font_family_str,
#1693
fontSize = table_font_size
),
# borders in the body
rowStyle = list(
fontWeight = table_body_font_weight,
borderTopStyle = table_body_hlines_style,
borderTopColor = table_body_hlines_color,
borderTopWidth = table_body_hlines_width,
borderBottomStyle = table_body_hlines_style,
borderBottomColor = table_body_hlines_color,
borderBottomWidth = table_body_hlines_width,
BorderRightStyle = table_body_vlines_style,
BorderRightColor = table_body_vlines_color,
BorderRightWidth = table_body_vlines_width,
BorderLeftStyle = table_body_vlines_style,
BorderLeftColor = table_body_vlines_color,
BorderLeftWidth = table_body_vlines_width
),
# cells_column_labels()
headerStyle = list(
fontWeight = column_labels_font_weight,
backgroundColor = column_labels_background_color,
borderBottomStyle = column_labels_border_bottom_style,
borderBottomWidth = column_labels_border_bottom_width,
borderBottomColor = column_labels_border_bottom_color,
borderTopColor = tbl_opts$column_labels_border_top_color
#
#borderTopColor = "transparent",
#borderTopStyle = "none"
),
# individually defined for the margins left+right
# cells_spanner_labels() styling
groupHeaderStyle = list(
fontWeight = column_labels_font_weight,
backgroundColor = column_labels_background_color,
borderBottomStyle = column_labels_border_bottom_style,
borderBottomWidth = column_labels_border_bottom_width,
borderBottomColor = column_labels_border_bottom_color,
borderTopColor = tbl_opts$column_labels_border_top_color
),
# body = table
tableStyle = list(
borderRightStyle = tbl_opts$table_body_vlines_style,
borderRightColor = tbl_opts$table_body_vlines_color,
borderRightWidth = tbl_opts$table_body_vlines_width,
borderLeftStyle = tbl_opts$table_body_vlines_style,
borderLeftColor = tbl_opts$table_body_vlines_color,
borderLeftWidth = tbl_opts$table_body_vlines_width,
# borderRightStyle = tbl_opts$table_border_right_style,
# borderRightColor = tbl_opts$table_border_right_color,
# borderRightWidth = tbl_opts$table_border_right_width,
# borderLeftStyle = tbl_opts$table_border_left_style,
# borderLeftColor = tbl_opts$table_border_left_color,
# borderLeftWidth = tbl_opts$table_border_left_width,
borderTopStyle = tbl_opts$table_border_top_style,
borderTopColor = tbl_opts$table_border_top_color,
borderTopWidth = tbl_opts$table_border_top_width,
borderBottomStyle = tbl_opts$table_border_bottom_style,
borderBottomColor = tbl_opts$table_border_bottom_color,
borderBottomWidth = tbl_opts$table_border_bottom_width
),
# stub styling?
# Also, rowGroupStyle isn't named or documented well I've realized. "Row group" in that context means a single row including the expandable details.
# rowStyle does the same thing, but does not include expandable details.
# I don't really use expandable details
# rowGroupStyle = list(
# backgroundColor = row_group_background_color,
# fontWeight = row_group_font_weight
# ),
# exclude pagination and search
tableBodyStyle = list(
borderTopStyle = tbl_opts$table_body_border_top_style,
borderTopColor = tbl_opts$table_body_border_top_color,
borderTopWidth = tbl_opts$table_body_border_top_width,
borderBottomStyle = tbl_opts$table_body_border_bottom_style,
borderBottomColor = tbl_opts$table_body_border_bottom_color,
borderBottomWidth = tbl_opts$table_body_border_bottom_width
),
rowStripedStyle = NULL,
rowHighlightStyle = NULL,
rowSelectedStyle = NULL,
# cells_body styling
# cellStyle = list(
# fontWeight = table_body_font_weight,
# backgroundColor = table_background_color
# ),
# grand_summary style
footerStyle = NULL,
inputStyle = NULL,
filterInputStyle = NULL,
searchInputStyle = NULL,
selectStyle = NULL,
paginationStyle = NULL,
pageButtonStyle = NULL,
pageButtonHoverStyle = NULL,
pageButtonActiveStyle = NULL,
pageButtonCurrentStyle = NULL
)
# Create the htmlwidget
x <-
reactable::reactable(
data = data_tbl,
columns = col_defs,
columnGroups = colgroups_def,
rownames = rownames_to_stub,
groupBy = groupname_col,
sortable = use_sorting,
resizable = use_resizers,
filterable = use_filters,
searchable = use_search,
searchMethod = NULL,
defaultColDef = default_col_def,
defaultColGroup = NULL,
defaultSortOrder = "asc",
defaultSorted = NULL,
pagination = use_pagination,
defaultPageSize = page_size_default,
showPageSizeOptions = use_page_size_select,
pageSizeOptions = page_size_values,
paginationType = pagination_type,
showPagination = use_pagination,
showPageInfo = use_pagination_info,
minRows = 1,
paginateSubRows = TRUE,
details = NULL,
defaultExpanded = expand_groupname_col,
selection = selection_mode,
selectionId = NULL,
defaultSelected = NULL,
onClick = onClick,
highlight = use_highlight,
outlined = FALSE,
# equivalent to opt_table_lines(extent = "all")
bordered = all_borders,
# equivalent to opt_table_lines(extent = "none")
borderless = borderless_borders,
striped = use_row_striping,
compact = use_compact_mode,
wrap = use_text_wrapping,
showSortIcon = TRUE,
showSortable = FALSE,
class = "gt_table",
style = NULL,
rowClass = NULL,
rowStyle = NULL,
fullWidth = TRUE,
width = table_width,
height = height,
theme = tbl_theme,
language = lang_defs,
elementId = id,
static = FALSE
)
#nocov start
# Prepend the `heading_component` to the widget content
if (!is.null(heading_component)) {
x <- htmlwidgets::prependContent(x, heading_component)
}
# Append the `footer_component` to the widget content
if (!is.null(footer_component)) {
x <- htmlwidgets::appendContent(x, footer_component)
}
#nocov end
x
}
create_source_notes_component_ihtml <- function(data) {
source_notes <- dt_source_notes_get(data = data)
if (is.null(source_notes)) {
return("")
}
styles_tbl <- dt_styles_get(data = data)
# Get the style attrs for the source notes
if ("source_notes" %in% styles_tbl$locname) {
source_notes_style <-
vctrs::vec_slice(
styles_tbl$html_style,
!is.na(styles_tbl$locname) & styles_tbl$locname == "source_notes"
)
source_notes_styles <-
if (length(source_notes_style) > 0) {
paste(source_notes_style, collapse = " ")
} else {
NULL
}
} else {
source_notes_styles <- NULL
}
tbl_opts <- dt_options_get_values(data)
# Get the source note multiline option
multiline <- tbl_opts$source_notes_multiline
# Get the source note separator option
separator <- tbl_opts$source_notes_sep
# Handle the multiline source notes case (each footnote takes up one line)
if (multiline) {
# Create the source notes component as a series of `<div>`s (one per
# source note) inside of a `<div>`
return(
htmltools::tags$div(
class = "gt_sourcenotes",
lapply(
source_notes,
function(x) {
htmltools::tags$div(
class = "gt_sourcenote",
style = source_notes_styles,
htmltools::HTML(x)
)
}
)
)
)
}
# Perform HTML escaping on the separator text and transform space
# characters to non-breaking spaces
separator <- gsub(" (?= )", " ", separator, perl = TRUE)
# Create the source notes component as a set of nested `<div>`s
htmltools::tags$div(
class = "gt_sourcenotes",
style = source_notes_styles,
htmltools::tags$div(
class = "gt_sourcenote",
htmltools::tags$div(
style = htmltools::css(`padding-bottom` = "2px"),
htmltools::HTML(paste(source_notes, collapse = separator))
)
)
)
}
create_footnotes_component_ihtml <- function(data) {
footnotes_tbl <- dt_footnotes_get(data = data)
# If the `footnotes_resolved` object has no
# rows, then return an empty footnotes component
if (nrow(footnotes_tbl) == 0) {
return("")
}
styles_tbl <- dt_styles_get(data = data)
# Get the distinct set of `fs_id` & `footnotes` values in the `footnotes_tbl`
footnotes_tbl <- dplyr::distinct(footnotes_tbl, fs_id, footnotes)
# Get the style attrs for the footnotes
if ("footnotes" %in% styles_tbl$locname) {
footnotes_style <- styles_tbl[styles_tbl$locname == "footnotes", ]
footnotes_styles <-
if (nrow(footnotes_style) > 0) {
paste(footnotes_style$html_style, collapse = " ")
} else {
NULL
}
} else {
footnotes_styles <- NULL
}
tbl_opts <- dt_options_get_values(data)
# Get the footnote multiline option
multiline <- tbl_opts$footnotes_multiline
# Get the footnote separator option
separator <- tbl_opts$footnotes_sep
# Obtain vectors of footnote ID values (prerendered glyphs) and
# the associated text
footnote_ids <- footnotes_tbl[["fs_id"]]
footnote_text <- footnotes_tbl[["footnotes"]]
# Create a vector of HTML footnotes
footnotes <-
unlist(
mapply(
SIMPLIFY = FALSE,
USE.NAMES = FALSE,
footnote_ids,
footnote_text,
FUN = function(x, footnote_text) {
as.character(