-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathrender_as_html.R
More file actions
73 lines (58 loc) · 1.9 KB
/
render_as_html.R
File metadata and controls
73 lines (58 loc) · 1.9 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
#' Transform a **gt** table object to an HTML table
#'
#' Take a `gt_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_html <- function(data) {
data <- build_data(data = data, context = "html")
# Composition of HTML -----------------------------------------------------
# Upgrade `_styles` to gain a `html_style` column with CSS style rules
data <- add_css_styles(data = data)
caption_component <- create_caption_component_h(data = data)
# Create the heading component
heading_component <- create_heading_component_h(data = data)
# Create the columns component
columns_component <- create_columns_component_h(data = data)
# Create the body component
body_component <- create_body_component_h(data = data)
# Create the source notes component
source_notes_component <- create_source_notes_component_h(data = data)
# Create the footnotes component
footnotes_component <- create_footnotes_component_h(data = data)
# Get attributes for the gt table
table_defs <- get_table_defs(data = data)
# Compose the HTML table
finalize_html_table(
class = "gt_table",
style = table_defs$table_style,
id = if (check_quarto()) paste0("table-", dt_options_get_value(data, "table_id")),
caption_component,
table_defs$table_colgroups,
heading_component,
columns_component,
body_component,
source_notes_component,
footnotes_component
)
}
finalize_html_table <- function(
class,
style,
...) {
html_tbl <-
as.character(
htmltools::tags$table(
class = "gt_table",
style = style,
...
)
)
# Unescape single quotes that may present as HTML entities (this is
# needed since the CSS inliner cannot parse "'")
html_tbl <- gsub("'", "'", html_tbl)
html_tbl
}