Skip to content

Commit

Permalink
Add gt support
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed May 14, 2024
1 parent 92f65bc commit 2aa740b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Imports:
vctrs
Suggests:
ggplot2,
gt,
knitr,
patchwork,
ragg,
Expand Down
7 changes: 7 additions & 0 deletions R/grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@
#' the aspect ratio will match the raster, otherwise the aspect ratio will be
#' taken from the styling of the element (defaults to 1.65)
#'
#' # Table rendering
#' While marquee does not support the extended table syntax for markdown it does
#' allow you to include tables in the output. It does so by supporting gt
#' objects as valid paths in image tags in the same way as ggplots etc. This
#' meeans that you can style your tables any way you wish and with the full
#' power of gt, which is much more flexible than the markdown table syntax.
#'
#' # Textbox justification
#' The justification options exceeds the classic ones provided by grid. While
#' numeric values are available as always, the number of possible text values
Expand Down
6 changes: 6 additions & 0 deletions R/images.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ images_as_grobs <- function(paths, env = caller_env()) {
check_installed("ggplot2")
obj <- ggplot2::ggplotGrob(obj)
}
if (inherits(obj, "gt_tbl")) {
check_installed("gt")
if ("as_gtable" %in% getNamespaceExports("gt")) {
obj <- gt::as_gtable(obj)
}
}
if (is.null(obj) || !is.grob(obj)) {
obj <- missing_grob()
}
Expand Down
10 changes: 8 additions & 2 deletions tests/testthat/test-images.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ test_that("images are picked up", {
grob <- textGrob("test")
gg <- ggplot2::ggplot()
patch <- patchwork::wrap_plots(gg, gg)
table <- gt::gt(mtcars)
unknown <- "test"

image_locs <- c(logo, "grob", "gg", "patch", unknown)
image_locs <- c(logo, "grob", "gg", "patch", "table", unknown)

imgs <- images_as_grobs(image_locs)

Expand All @@ -20,5 +21,10 @@ test_that("images are picked up", {
expect_s3_class(imgs[[2]], "text")
expect_s3_class(imgs[[3]], "gtable")
expect_s3_class(imgs[[4]], "gtable")
expect_s3_class(imgs[[5]], "missing_grob")
expect_s3_class(imgs[[6]], "missing_grob")
if ("as_gtable" %in% getNamespaceExports("gt")) {
expect_s3_class(imgs[[5]], "gtable")
} else {
expect_s3_class(imgs[[5]], "missing_grob")
}
})
44 changes: 43 additions & 1 deletion vignettes/marquee.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ knitr::opts_chunk$set(
dpi = 300
)
should_eval <- getRversion() >= "4.3"
has_gt_gtable <- "as_gtable" %in% getNamespaceExports("gt")
```

```{r setup}
Expand Down Expand Up @@ -133,7 +134,7 @@ Another use of the geom, where rich text may come more into play, is in annotati
text_box_style <- modify_style(
classic_style(base_size = 2),
"body",
padding = skip_inherit(trbl(0, 10, 10)),
padding = skip_inherit(trbl(10)),
border_radius = 3
)
Expand Down Expand Up @@ -229,6 +230,47 @@ laborum."
grid.draw(marquee_grob(md_text_plot, classic_style()))
```

## A bit about tables

The table markdown syntax is not supported in marquee. However, you can still include tables in marquee, and with much more styling support than standard markdown table syntax provide. This magic is an extension of what was talked about above. More to the point, gt table objects are recognized as valid objects in the image tag path and can thus be included directly in the output. We show the power here with a table specification taken from the [gt introduction](https://gt.rstudio.com/articles/gt.html):

```{r, collapse=TRUE}
library(gt)
airquality_m <- airquality[1:10, ]
airquality_m$Year <- 1973L
table <- gt(airquality_m)
table <- tab_header(table,
title = "New York Air Quality Measurements",
subtitle = "Daily measurements in New York City (May 1-10, 1973)"
)
table <- tab_spanner(table,
label = "Time",
columns = c(Year, Month, Day)
)
table <- tab_spanner(table,
label = "Measurement",
columns = c(Ozone, Solar.R, Wind, Temp)
)
```

```{block, include=!has_gt_gtable}
> This vignette has been rendered with an older version of gt that does not support converting gt objects to gtables. Because of this you will see a "missing grob" in the example below
```

```{r, fig.asp=1.1, eval=should_eval}
md_text_table <-
"# Lorem Ipsum
Below we have a table created with gt
![](table)
*Such tables, much cells*"
grid.draw(marquee_grob(md_text_table, classic_style()))
```

The support is still somewhat experimental as gt-to-grob conversion is getting build out. Some gt features rely on HTML formatting still and will end up looking weird, but standard table inclusion works as expected.

## Wrapping up

This should give you enough of an overview to get started. The Marquee Style and Marquee Syntax vignettes provides a bit more detail on their respective areas if you need to dive deeper.

0 comments on commit 2aa740b

Please sign in to comment.