Skip to content
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ Collate:
'helpers.R'
'image.R'
'info_tables.R'
'knitr-utils.R'
'location_methods.R'
'modify_columns.R'
'modify_rows.R'
Expand Down
1 change: 1 addition & 0 deletions R/dt_options.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ dt_options_tbl <-
"container_overflow_x", FALSE, "container", "overflow","auto",
"container_overflow_y", FALSE, "container", "overflow","auto",
"table_id", FALSE, "table", "value", NA_character_,
"table_caption", FALSE, "table", "value", NULL,
"table_width", TRUE, "table", "px", "auto",
"table_layout", TRUE, "table", "value", "fixed",
"table_margin_left", TRUE, "table", "px", "auto",
Expand Down
24 changes: 23 additions & 1 deletion R/gt.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#' group labels for generation of stub row groups. If the input `data` table
#' has the `grouped_df` class (through use of the [dplyr::group_by()] function
#' or associated `group_by*()` functions) then any input here is ignored.
#' @param caption An optional table caption to use for cross-referencing
#' in R Markdown documents and **bookdown** book projects.
#' @param rownames_to_stub An option to take rownames from the input `data`
#' table as row captions in the display table stub.
#' @param auto_align Optionally have column data be aligned depending on the
Expand Down Expand Up @@ -82,6 +84,7 @@
gt <- function(data,
rowname_col = "rowname",
groupname_col = dplyr::group_vars(data),
caption = NULL,
rownames_to_stub = FALSE,
auto_align = TRUE,
id = NULL,
Expand Down Expand Up @@ -144,7 +147,26 @@ gt <- function(data,
# Add any user-defined table ID to the `table_id` parameter
# (if NULL, the default setting will generate a random ID)
if (!is.null(id)) {
data <- dt_options_set_value(data = data, option = "table_id", value = id)
data <-
dt_options_set_value(
data = data,
option = "table_id",
value = id
)
}

# Add any user-defined table caption to the `table_id` parameter
# TODO: consider whether this might take a string or a logical (to say that
# we'll use the header from `tab_header` as the table caption); this might
# require some more thought still because a `caption` arg might also be
# sensible in `tab_header`
if (!is.null(caption)) {
data <-
dt_options_set_value(
data = data,
option = "table_caption",
value = caption
)
}

# Apply the `gt_tbl` class to the object while
Expand Down
22 changes: 22 additions & 0 deletions R/knitr-utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# These functions are copies of unexported functions in knitr

kable_caption = function(label, caption, format) {
# create a label for bookdown if applicable
if (is.null(label)) label = knitr::opts_current$get('label')
if (!is.null(caption) && !is.na(caption)) caption = paste0(
create_label('tab:', label, latex = (format == 'latex')), caption
)
caption
}

# create \label{x} or (\#x); the latter is current an internal hack for bookdown
create_label = function(..., latex = FALSE) {
if (isTRUE(knitr::opts_knit$get('bookdown.internal.label'))) {
lab1 = '(\\#'; lab2 = ')'
} else if (latex) {
lab1 = '\\label{'; lab2 = '}'
} else {
return('') # we don't want the label at all
}
paste(c(lab1, ..., lab2), collapse = '')
}
3 changes: 3 additions & 0 deletions R/render_as_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ render_as_html <- function(data) {
# 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(data = data, context = "html")

Expand All @@ -39,6 +41,7 @@ render_as_html <- function(data) {
htmltools::tags$table(
class = "gt_table",
style = table_defs$table_style,
caption_component,
table_defs$table_colgroups,
heading_component,
columns_component,
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ process_text <- function(text,

return(text)

} else if (is_html(text)) {
} else if (is_html(text) || inherits(text, "shiny.tag") || inherits(text, "shiny.tag.list")) {

text <- as.character(text)

Expand Down
33 changes: 33 additions & 0 deletions R/utils_render_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,39 @@ get_table_defs <- function(data) {
)
}

create_caption_component_h <- function(data) {

# Create the table caption if available
table_caption <- dt_options_get_value(data = data, option = "table_caption")

if (!is.null(table_caption)) {
table_caption <- process_text(table_caption, context = "html")
if (isTRUE(getOption("knitr.in.progress"))) {
table_caption <- kable_caption(label = NULL, table_caption, "html")
}
# <!--/html_preserve--> ... <!--html_preserve--> is because bookdown scans
# the .md file, looking for references in the form of:
# <caption>(#tab:mytable)
# Ref:
# https://github.com/rstudio/bookdown/blob/00987215b7572def2f5cd73a623efc38f4f30ab7/R/html.R#L629
# https://github.com/rstudio/bookdown/blob/00987215b7572def2f5cd73a623efc38f4f30ab7/R/html.R#L667
#
# Normally, the gt table in its entirety is excluded from the .md, to
# prevent it from being corrupted by pandoc's md-to-html rendering. We do
# this by wrapping the whole table in htmltools::htmlPreserve (I think this
# actually happens in htmlwidgets). So the extra markup here is used to
# temporarily suspend that protection, emit the caption (including the HTML
# <caption> tag, which bookdown searches for), and then resume protection.
htmltools::HTML(paste0(
"<!--/html_preserve--><caption>",
table_caption,
"</caption><!--html_preserve-->"
))
} else {
NULL
}
}

#' Create the heading component of a table
#'
#' The table heading component contains the title and possibly a subtitle; if
Expand Down
4 changes: 4 additions & 0 deletions man/gt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/helper-gt_attr_expectations.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ expect_tab <- function(tab,

dt_options_get(data = tab) %>%
dim() %>%
expect_equal(c(133, 5))
expect_equal(c(134, 5))

dt_transforms_get(data = tab) %>%
length() %>%
Expand Down
50 changes: 50 additions & 0 deletions tests/testthat/test-utils_render_html.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
expect_caption_eq <- function(caption, expected) {
result <- create_caption_component_h(gt(exibble, caption = caption))
expect_identical(
result,
htmltools::HTML(paste0(
"<!--/html_preserve--><caption>",
expected,
"</caption><!--html_preserve-->"
))
)
}

test_that("captioning processes text correctly", {

# No caption if not specified
expect_null(create_caption_component_h(exibble %>% gt()))

expect_caption_eq("**hello & goodbye**", "**hello &amp; goodbye**")
expect_caption_eq(md("**hello & goodbye**"), "<strong>hello &amp; goodbye</strong>")
expect_caption_eq(I("**hello & goodbye**"), "**hello & goodbye**")
expect_caption_eq(htmltools::strong("hello & goodbye"), "<strong>hello &amp; goodbye</strong>")
expect_caption_eq(htmltools::HTML("<strong>hello &amp; goodbye</strong>"), "<strong>hello &amp; goodbye</strong>")
expect_caption_eq(I("<strong>hello &amp; goodbye</strong>"), "<strong>hello &amp; goodbye</strong>")

expect_caption_eq("", "")
})

test_that("bookdown-style crossrefs are added when appropriate", {
op <- options(knitr.in.progress = TRUE)
on.exit(options(op), add = TRUE)

stopifnot(is.null(knitr::opts_current$get("label")))
stopifnot(is.null(knitr::opts_knit$get("bookdown.internal.label")))
on.exit({
knitr::opts_current$set(label = NULL)
knitr::opts_knit$set(bookdown.internal.label = NULL)
}, add = TRUE)

knitr::opts_current$set(label = "foo")

# If bookdown, then ref is generated
knitr::opts_knit$set(bookdown.internal.label = TRUE)
expect_caption_eq("test", "(\\#tab:foo)test")

expect_null(create_caption_component_h(exibble %>% gt()))

# If bookdown.internal.label is unset, ref is not generated
knitr::opts_knit$set(bookdown.internal.label = NULL)
expect_caption_eq("test", "test")
})