-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathprint.R
More file actions
146 lines (118 loc) · 3.72 KB
/
print.R
File metadata and controls
146 lines (118 loc) · 3.72 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
#' Print the table
#'
#' This facilitates printing of the HTML table to the R console.
#'
#' @param x An object of class `gt_tbl`.
#' @param ... Any additional parameters.
#' @param view The value for `print()`s `browse` argument.
#'
#' @keywords internal
#'
#' @export
print.gt_tbl <- function(x, ..., view = interactive()) {
html_tbl <- as.tags.gt_tbl(x, ...)
# Use `print()` to print to the console
print(html_tbl, browse = view, ...)
}
knitr_is_rtf_output <- function() {
"rtf" %in% knitr::opts_knit$get("rmarkdown.pandoc.to")
}
knitr_is_word_output <- function() {
"docx" %in% knitr::opts_knit$get("rmarkdown.pandoc.to")
}
#' Knit print the table
#'
#' This facilitates printing of the HTML table within a knitr code chunk.
#'
#' @param x An object of class `gt_tbl`.
#' @param ... Any additional parameters.
#'
#' @keywords internal
#' @noRd
knit_print.gt_tbl <- function(x, ...) {
if (check_quarto()) {
caption_text <- dt_options_get_value(data = x, option = "table_caption")
table_uuid <- random_id()
x <- dt_options_set_value(data = x, option = "table_id", value = table_uuid)
if (!is.na(caption_text)) {
quarto_api_send(
"set_table_caption",
caption = caption_text,
table_id = paste0("table-", table_uuid))
}
}
if (knitr_is_rtf_output()) {
x <- as_rtf(x)
} else if (knitr::is_latex_output()) {
x <- as_latex(x)
} else if (knitr_is_word_output()) {
x <-
paste("```{=openxml}", as_word(x), "```\n\n", sep = "\n") %>%
knitr::asis_output()
} else {
# Default to HTML output
x <- as.tags.gt_tbl(x, ...)
}
# Use `knit_print()` to print in a code chunk
knitr::knit_print(x, ...)
}
#' Convert a **gt** table to an **htmltools** `tagList`
#'
#' This converts a **gt** table object to an **htmltools**
#' [htmltools::tagList()] object. The returned object is of the `shiny.tag.list`
#' class and using `as.character()` with that will render the HTML, resulting in
#' a length 1 character vector that contains the HTML table.
#'
#' @param x Object to be converted.
#' @param ... Any additional parameters.
#'
#' @keywords internal
#' @noRd
as.tags.gt_tbl <- function(x, ...) {
table_id <- dt_options_get_value(x, option = "table_id")
if (is.na(table_id)) {
id <- random_id()
} else {
id <- table_id
}
# Generate the HTML table
html_table <- render_as_html(data = x)
# Compile the SCSS as CSS
css <- compile_scss(data = x, id = id)
# Get options related to the enclosing <div>
container_padding_x <- dt_options_get_value(x, option = "container_padding_x")
container_padding_y <- dt_options_get_value(x, option = "container_padding_y")
container_overflow_x <- dt_options_get_value(x, option = "container_overflow_x")
container_overflow_y <- dt_options_get_value(x, option = "container_overflow_y")
container_width <- dt_options_get_value(x, option = "container_width")
container_height <- dt_options_get_value(x, option = "container_height")
# Attach the dependency to the HTML table
html_tbl <-
htmltools::tags$div(
id = id,
htmltools::tags$style(htmltools::HTML(css)),
style = htmltools::css(
`padding-left` = container_padding_x,
`padding-right` = container_padding_x,
`padding-top` = container_padding_y,
`padding-bottom` = container_padding_y,
`overflow-x` = container_overflow_x,
`overflow-y` = container_overflow_y,
width = container_width,
height = container_height
),
htmltools::HTML(html_table)
)
html_tbl
}
#' Print RTF text
#'
#' @param x Object to be printed.
#' @param ... Any additional parameters.
#'
#' @keywords internal
#'
#' @export
print.rtf_text <- function(x, ...) {
cat(paste(x, collapse = "\n"))
}