forked from ddsjoberg/gtsummary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print.R
118 lines (108 loc) · 4 KB
/
print.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
#' print and knit_print methods for gtsummary objects
#'
#' @name print_gtsummary
#' @keywords internal
#' @param x An object created using gtsummary functions
#' @param print_engine String indicating the print method. Must be one of
#' `"gt"`, `"kable"`, `"kable_extra"`, `"flextable"`, `"tibble"`
#' @param ... Not used
#' @author Daniel D. Sjoberg
#' @seealso [tbl_summary] [tbl_regression] [tbl_uvregression] [tbl_merge] [tbl_stack]
NULL
#' @rdname print_gtsummary
#' @export
print.gtsummary <- function(x, print_engine = NULL, ...) {
check_dots_empty(error = function(e) inform(c(e$message, e$body)))
# select print engine
print_engine <-
print_engine %||%
.get_deprecated_option("gtsummary.print_engine") %||%
get_theme_element("pkgwide-str:print_engine") %||%
"gt" # default printer is gt
# checking engine
accepted_print_engines <-
c("gt", "kable", "kable_extra", "flextable", "huxtable", "tibble")
if (!rlang::is_string(print_engine) || !print_engine %in% accepted_print_engines) {
stop(glue(
"Select a valid print engine. ",
"Please select one of {quoted_list(accepted_print_engines)}"
))
}
# printing results
switch(print_engine,
"gt" = as_gt(x),
"kable" = as_kable(x),
"flextable" = as_flex_table(x),
"kable_extra" = as_kable_extra(x),
"huxtable" = as_hux_table(x),
"tibble" = as_tibble(x)
) %>%
print()
}
#' @rdname print_gtsummary
#' @export
knit_print.gtsummary <- function(x, ...) {
# assigning print engine -----------------------------------------------------
# select print engine
print_engine <-
.get_deprecated_option("gtsummary.print_engine") %||%
get_theme_element("pkgwide-str:print_engine")
# gt is the default printer for html output
if (is.null(print_engine) && knitr::is_html_output() == TRUE) {
print_engine <- "gt"
}
# PDF uses kable as default printer (is_latex_output catches pdf_document and beamer...maybe more?)
else if (is.null(print_engine) && knitr::is_latex_output() == TRUE) {
rlang::inform(paste(
"Table printed with `knitr::kable()`, not {gt}. Learn why at",
"https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html",
"To suppress this message, include `message = FALSE` in code chunk header.",
sep = "\n"
))
print_engine <- "kable"
}
# don't use word_document with gt engine
else if (identical(print_engine %||% "gt", "gt") &&
"docx" %in% knitr::opts_knit$get("rmarkdown.pandoc.to")) {
if (assert_package("flextable", boolean = TRUE)) {
rlang::inform(paste(
"Table printed with {flextable}, not {gt}. Learn why at",
"https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html",
"To suppress this message, include `message = FALSE` in the code chunk header.",
sep = "\n"
))
print_engine <- "flextable"
} else {
rlang::inform(paste(
"Table printed with `knitr::kable()`, not {gt}. Learn why at",
"https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html",
"To suppress this message, include `message = FALSE` in the code chunk header.",
sep = "\n"
))
print_engine <- "kable"
}
}
# RTF warning when using gt
else if (identical(print_engine %||% "gt", "gt") &&
"rtf" %in% knitr::opts_knit$get("rmarkdown.pandoc.to")) {
rlang::inform(paste(
"Table printed with `knitr::kable()`, not {gt}. Learn why at",
"https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html",
"To suppress this message, include `message = FALSE` in code chunk header.",
sep = "\n"
))
print_engine <- "kable"
}
# all other types (if any), will attempt to print with gt
else if (is.null(print_engine)) print_engine <- "gt"
# printing gtsummary table with appropriate engine
switch(print_engine,
"gt" = as_gt(x),
"kable" = as_kable(x),
"flextable" = as_flex_table(x),
"kable_extra" = as_kable_extra(x),
"huxtable" = as_hux_table(x),
"tibble" = as_tibble(x)
) %>%
knitr::knit_print()
}