From 1b79a079c730abd58af60c42acea9ccfe74a089c Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Tue, 23 Apr 2024 17:34:26 -0500 Subject: [PATCH] Render gt tables Fixes #2326 --- DESCRIPTION | 1 + NAMESPACE | 1 + NEWS.md | 1 + R/pkgdown_print.R | 12 ++++++++++++ R/test.R | 9 +++++++++ inst/BS5/assets/pkgdown.scss | 6 ++++++ man/index.Rd | 1 + man/test-crayon.Rd | 1 + man/test-dont.Rd | 1 + man/test-figures.Rd | 1 + man/test-links.Rd | 1 + man/test-lists.Rd | 1 + man/test-long-lines.Rd | 1 + man/test-output-styles.Rd | 1 + man/test-params.Rd | 1 + man/test-sexpr-title.Rd | 1 + man/test-tables.Rd | 27 +++++++++++++++++++++++++++ man/test-verbatim.Rd | 3 ++- 18 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 man/test-tables.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 7222eeaaa..379a92463 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -45,6 +45,7 @@ Suggests: diffviewer, evaluate, gert, + gt, htmltools, htmlwidgets, knitr, diff --git a/NAMESPACE b/NAMESPACE index 7ae939ddb..cbd7245aa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -89,6 +89,7 @@ S3method(as_html,tag_url) S3method(as_html,tag_var) S3method(as_html,tag_verb) S3method(pkgdown_print,default) +S3method(pkgdown_print,gt_tbl) S3method(pkgdown_print,htmlwidget) S3method(print,pkgdown_xml) S3method(print,print_yaml) diff --git a/NEWS.md b/NEWS.md index b582dc5da..91aee736a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,6 @@ # pkgdown (development version) +* `build_reference()` now automatically renders any tables created by gt (#2326). * Very wide words are now automatically broken across lines and hyphenated (when possible) when they'd otherwise create a horizontal scrollbar on mobile (#1888). * The `repo.source.url` field no longer requires a trailing slash (#2017). * Anywhere you can use `_pkgdown.yml`, you can now use `_pkgdown.yaml` (#2244). diff --git a/R/pkgdown_print.R b/R/pkgdown_print.R index 0a107cee8..f0e6f4ea8 100644 --- a/R/pkgdown_print.R +++ b/R/pkgdown_print.R @@ -40,3 +40,15 @@ pkgdown_print.htmlwidget <- function(x, visible = TRUE) { x$height <- x$height %||% (settings$fig.height * settings$dpi) x } + +#' @export +pkgdown_print.gt_tbl <- function(x, visible = TRUE) { + if (!visible) { + return(invisible()) + } + + htmltools::div( + class = "gt-table", + gt::as_raw_html(x) + ) +} diff --git a/R/test.R b/R/test.R index e63affb14..366596151 100644 --- a/R/test.R +++ b/R/test.R @@ -82,6 +82,15 @@ NULL #' text(2, 5, "Hello", srt = 30, cex = 2) NULL +#' Test case: tables +#' +#' @name test-tables +#' @keywords internal +#' @family tests +#' @examples +#' gt::gt(head(mtcars)) +NULL + #' Test case: don't #' #' @name test-dont diff --git a/inst/BS5/assets/pkgdown.scss b/inst/BS5/assets/pkgdown.scss index 5b97fce38..ccfe4e1b9 100644 --- a/inst/BS5/assets/pkgdown.scss +++ b/inst/BS5/assets/pkgdown.scss @@ -420,6 +420,12 @@ pre { padding: 1rem 0.5rem; } +// Spacing tweaks for gt table +pre div.gt-table { + white-space: normal; + margin-top: 1rem; +} + // "Pop" code out of page margins on small screens to give a little more room @include media-breakpoint-down(sm) { // div.section div.sourceCode pre diff --git a/man/index.Rd b/man/index.Rd index fd33381c6..71dc7bcba 100644 --- a/man/index.Rd +++ b/man/index.Rd @@ -18,6 +18,7 @@ Other tests: \code{\link{test-output-styles}}, \code{\link{test-params}}, \code{\link{test-sexpr-title}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-crayon.Rd b/man/test-crayon.Rd index 3a159568b..aa779cb85 100644 --- a/man/test-crayon.Rd +++ b/man/test-crayon.Rd @@ -25,6 +25,7 @@ Other tests: \code{\link{test-output-styles}}, \code{\link{test-params}}, \code{\link{test-sexpr-title}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-dont.Rd b/man/test-dont.Rd index 804b68984..a8be6493b 100644 --- a/man/test-dont.Rd +++ b/man/test-dont.Rd @@ -56,6 +56,7 @@ Other tests: \code{\link{test-output-styles}}, \code{\link{test-params}}, \code{\link{test-sexpr-title}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-figures.Rd b/man/test-figures.Rd index 5ca5d8b37..208b1a3e6 100644 --- a/man/test-figures.Rd +++ b/man/test-figures.Rd @@ -25,6 +25,7 @@ Other tests: \code{\link{test-output-styles}}, \code{\link{test-params}}, \code{\link{test-sexpr-title}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-links.Rd b/man/test-links.Rd index 493b035e9..6c6a9b3af 100644 --- a/man/test-links.Rd +++ b/man/test-links.Rd @@ -25,6 +25,7 @@ Other tests: \code{\link{test-output-styles}}, \code{\link{test-params}}, \code{\link{test-sexpr-title}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-lists.Rd b/man/test-lists.Rd index 88cbc3a54..edef2f8c7 100644 --- a/man/test-lists.Rd +++ b/man/test-lists.Rd @@ -58,6 +58,7 @@ Other tests: \code{\link{test-output-styles}}, \code{\link{test-params}}, \code{\link{test-sexpr-title}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-long-lines.Rd b/man/test-long-lines.Rd index 869cd3888..39cd7cb03 100644 --- a/man/test-long-lines.Rd +++ b/man/test-long-lines.Rd @@ -26,6 +26,7 @@ Other tests: \code{\link{test-output-styles}}, \code{\link{test-params}}, \code{\link{test-sexpr-title}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-output-styles.Rd b/man/test-output-styles.Rd index e7655192f..674cdc0ef 100644 --- a/man/test-output-styles.Rd +++ b/man/test-output-styles.Rd @@ -33,6 +33,7 @@ Other tests: \code{\link{test-long-lines}}, \code{\link{test-params}}, \code{\link{test-sexpr-title}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-params.Rd b/man/test-params.Rd index 3c75c26f7..3439897b3 100644 --- a/man/test-params.Rd +++ b/man/test-params.Rd @@ -20,6 +20,7 @@ Other tests: \code{\link{test-long-lines}}, \code{\link{test-output-styles}}, \code{\link{test-sexpr-title}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-sexpr-title.Rd b/man/test-sexpr-title.Rd index 039fbf19d..ad502afa3 100644 --- a/man/test-sexpr-title.Rd +++ b/man/test-sexpr-title.Rd @@ -17,6 +17,7 @@ Other tests: \code{\link{test-long-lines}}, \code{\link{test-output-styles}}, \code{\link{test-params}}, +\code{\link{test-tables}}, \code{\link{test-verbatim}} } \concept{tests} diff --git a/man/test-tables.Rd b/man/test-tables.Rd new file mode 100644 index 000000000..5a27a2ad9 --- /dev/null +++ b/man/test-tables.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/test.R +\name{test-tables} +\alias{test-tables} +\title{Test case: tables} +\description{ +Test case: tables +} +\examples{ +gt::gt(head(mtcars)) +} +\seealso{ +Other tests: +\code{\link{index}}, +\code{\link{test-crayon}}, +\code{\link{test-dont}}, +\code{\link{test-figures}}, +\code{\link{test-links}}, +\code{\link{test-lists}}, +\code{\link{test-long-lines}}, +\code{\link{test-output-styles}}, +\code{\link{test-params}}, +\code{\link{test-sexpr-title}}, +\code{\link{test-verbatim}} +} +\concept{tests} +\keyword{internal} diff --git a/man/test-verbatim.Rd b/man/test-verbatim.Rd index 96d22b3c3..a2d74128b 100644 --- a/man/test-verbatim.Rd +++ b/man/test-verbatim.Rd @@ -47,7 +47,8 @@ Other tests: \code{\link{test-long-lines}}, \code{\link{test-output-styles}}, \code{\link{test-params}}, -\code{\link{test-sexpr-title}} +\code{\link{test-sexpr-title}}, +\code{\link{test-tables}} } \concept{tests} \keyword{internal}