From 46a42f795597a3a8740fb22751ee78d7e648f0e3 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Thu, 26 Jan 2023 11:37:31 -0500 Subject: [PATCH 1/6] Add `quarto_api_send()` util function --- R/utils_quarto.R | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/R/utils_quarto.R b/R/utils_quarto.R index 4c6488d98c..117b341c4c 100644 --- a/R/utils_quarto.R +++ b/R/utils_quarto.R @@ -1,3 +1,35 @@ check_quarto <- function() { Sys.getenv("QUARTO_MESSAGES_FILE") != "" } + +check_knitr <- function() { + + if (!requireNamespace("knitr", quietly = TRUE)) { + + cli::cli_abort(c( + "Please install the knitr package.", + "*" = "Use `install.packages(\"knitr\")`." + )) + } +} + +quarto_api_send <- function(message, ...) { + + # Ensure that the knitr package is available + check_knitr() + + # Generate a JSON-formatted line + line <- + jsonlite::toJSON( + list( + message = message, + cell_name = knitr::opts_current$get("label"), + payload = list(...) + ), + auto_unbox = TRUE + ) + + file <- Sys.getenv("QUARTO_MESSAGES_FILE") + + write(line, file, append = TRUE) +} From 34c70b7460bc5b68b6f732d8781c7150a30bf0e8 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Thu, 26 Jan 2023 11:37:46 -0500 Subject: [PATCH 2/6] Send caption text to Quarto --- R/print.R | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/R/print.R b/R/print.R index c8f9f9ce0d..77deec24c6 100644 --- a/R/print.R +++ b/R/print.R @@ -38,6 +38,15 @@ knitr_is_word_output <- function() { #' @noRd knit_print.gt_tbl <- function(x, ...) { + if (check_quarto()) { + + caption_text <- dt_options_get_value(data = x, option = "table_caption") + + if (!is.na(caption_text)) { + quarto_api_send("set_cell_caption", caption_text) + } + } + if (knitr_is_rtf_output()) { x <- as_rtf(x) From 7854a9ff5a17652599b63353e8ff4fe41fa622ff Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Tue, 31 Jan 2023 11:39:36 -0800 Subject: [PATCH 3/6] uuid --- DESCRIPTION | 4 +++- R/utils_quarto.R | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 1eb0960cd4..2c97acceac 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -44,12 +44,14 @@ Imports: glue (>= 1.6.1), htmltools (>= 0.5.2), juicyjuice (>= 0.1.0), + jsonlite, magrittr (>= 2.0.2), rlang (>= 1.0.2), sass (>= 0.4.1), scales (>= 1.2.0), tibble (>= 3.1.6), - tidyselect (>= 1.1.1) + tidyselect (>= 1.1.1), + uuid Suggests: covr, digest (>= 0.6.29), diff --git a/R/utils_quarto.R b/R/utils_quarto.R index 117b341c4c..83e1f25861 100644 --- a/R/utils_quarto.R +++ b/R/utils_quarto.R @@ -13,8 +13,14 @@ check_knitr <- function() { } } +get_uuid <- function() { + uuid::UUIDgenerate() +} + quarto_api_send <- function(message, ...) { + uuid <- get_uuid() + # Ensure that the knitr package is available check_knitr() @@ -23,7 +29,7 @@ quarto_api_send <- function(message, ...) { jsonlite::toJSON( list( message = message, - cell_name = knitr::opts_current$get("label"), + uuid = uuid, payload = list(...) ), auto_unbox = TRUE From 38a03b65e59cd2876a66ef788fadac962e60b31b Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Tue, 31 Jan 2023 14:34:50 -0800 Subject: [PATCH 4/6] partial work --- R/print.R | 9 ++++++++- R/utils_quarto.R | 2 ++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/R/print.R b/R/print.R index 77deec24c6..60797d2fbd 100644 --- a/R/print.R +++ b/R/print.R @@ -41,9 +41,16 @@ knit_print.gt_tbl <- function(x, ...) { if (check_quarto()) { caption_text <- dt_options_get_value(data = x, option = "table_caption") + table_uuid <- uuid::UUIDgenerate() + dt_options_set_value(data = x, option = "table_id", value = table_uuid) + table_id <- dt_options_get_value(data = x, option = "table_id") + # FIX THIS if (!is.na(caption_text)) { - quarto_api_send("set_cell_caption", caption_text) + quarto_api_send( + "set_table_caption", + caption = caption_text, + table_id = table_uuid) } } diff --git a/R/utils_quarto.R b/R/utils_quarto.R index 83e1f25861..eaff7ed795 100644 --- a/R/utils_quarto.R +++ b/R/utils_quarto.R @@ -38,4 +38,6 @@ quarto_api_send <- function(message, ...) { file <- Sys.getenv("QUARTO_MESSAGES_FILE") write(line, file, append = TRUE) + + uuid } From bca3d418827baac8067547cda1db878513e39704 Mon Sep 17 00:00:00 2001 From: Carlos Scheidegger Date: Tue, 31 Jan 2023 15:40:17 -0800 Subject: [PATCH 5/6] set table id in addition to div id --- R/print.R | 9 +++------ R/render_as_html.R | 1 + 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/R/print.R b/R/print.R index 60797d2fbd..0c2a99ac0c 100644 --- a/R/print.R +++ b/R/print.R @@ -39,18 +39,15 @@ knitr_is_word_output <- function() { knit_print.gt_tbl <- function(x, ...) { if (check_quarto()) { - caption_text <- dt_options_get_value(data = x, option = "table_caption") - table_uuid <- uuid::UUIDgenerate() - dt_options_set_value(data = x, option = "table_id", value = table_uuid) - table_id <- dt_options_get_value(data = x, option = "table_id") - # FIX THIS + 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 = table_uuid) + table_id = paste0("table-", table_uuid)) } } diff --git a/R/render_as_html.R b/R/render_as_html.R index 7167733d9b..f9fcdcedcf 100644 --- a/R/render_as_html.R +++ b/R/render_as_html.R @@ -40,6 +40,7 @@ render_as_html <- function(data) { finalize_html_table( class = "gt_table", style = table_defs$table_style, + id = paste0("table-", dt_options_get_value(data, "table_id")), caption_component, table_defs$table_colgroups, heading_component, From 7950446aa11daf666d85bcd007a50b4896e35c51 Mon Sep 17 00:00:00 2001 From: Richard Iannone Date: Tue, 31 Jan 2023 15:52:15 -0800 Subject: [PATCH 6/6] Update render_as_html.R --- R/render_as_html.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/render_as_html.R b/R/render_as_html.R index f9fcdcedcf..ba4b509c5b 100644 --- a/R/render_as_html.R +++ b/R/render_as_html.R @@ -40,7 +40,7 @@ render_as_html <- function(data) { finalize_html_table( class = "gt_table", style = table_defs$table_style, - id = paste0("table-", dt_options_get_value(data, "table_id")), + id = if (check_quarto()) paste0("table-", dt_options_get_value(data, "table_id")), caption_component, table_defs$table_colgroups, heading_component,