From 0d29380263142154ccdd87c86517f4b582e683af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Wed, 8 Jul 2026 13:55:35 +0100 Subject: [PATCH 01/10] feat: implements render of tbl_split --- NAMESPACE | 4 +++ R/table_with_settings.R | 55 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/NAMESPACE b/NAMESPACE index df41b6ca..c54f43fa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,11 +5,15 @@ S3method(export_table,TableTree) S3method(export_table,default) S3method(export_table,gt_tbl) S3method(export_table,gtsummary) +S3method(export_table,tbl_split) +S3method(file_download_format,default) +S3method(file_download_format,tbl_split) S3method(render_table_to_html,ElementaryTable) S3method(render_table_to_html,TableTree) S3method(render_table_to_html,default) S3method(render_table_to_html,gt_tbl) S3method(render_table_to_html,gtsummary) +S3method(render_table_to_html,tbl_split) export(basic_table_args) export(clean_brushedPoints) export(draggable_buckets) diff --git a/R/table_with_settings.R b/R/table_with_settings.R index 6bc58ed8..2effba82 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -14,6 +14,25 @@ render_table_to_html_rtables <- function(x, ...) { rtables::as_html(x) } +file_download_format <- function(x, filename) { + UseMethod("file_download_format", x) +} + +#' @method file_download_format default +#' @keywords internal +#' @exportS3Method +file_download_format.default <- function(x, filename) { + filename +} + +#' @method file_download_format tbl_split +#' @keywords internal +#' @exportS3Method +file_download_format.tbl_split <- function(x, filename) { + new_filename <- tools::file_path_sans_ext(filename) + paste0(new_filename, ".zip") +} + #' Render table object to HTML #' #' @param x The table object to render @@ -61,6 +80,20 @@ render_table_to_html.gt_tbl <- function(x, ...) { htmltools::HTML(gt::as_raw_html(x)) } +#' @method render_table_to_html tbl_split +#' @keywords internal +#' @exportS3Method +render_table_to_html.tbl_split <- function(x, ...) { + tables <- lapply(seq_along(x), function(tbl) { + label <- attr(x[[tbl]], "variable_level", exact = TRUE) + htmltools::tags$div( + if (!rlang::is_empty(label)) htmltools::tags$h4("Variable level:", label), + teal.widgets:::render_table_to_html(x[[tbl]]) + ) + }) + htmltools::tags$div(tables) +} + #' Export table object to file #' #' @param x The table object to export @@ -148,6 +181,26 @@ export_table.gt_tbl <- function(x, file, format, paginate = FALSE, lpp = NULL, . } } +#' @method export_table tbl_split +#' @keywords internal +#' @exportS3Method +export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL, ...) { + ext <- format # ".pdf" or ".txt" + tmp_dir <- tempfile() + dir.create(tmp_dir) + on.exit(unlink(tmp_dir, recursive = TRUE)) + + base_name <- tools::file_path_sans_ext(basename(file)) + + tmp_files <- lapply(seq_along(x), function(i) { + tmp_file <- file.path(tmp_dir, paste0(base_name, "_", i, ext)) + export_table(x[[i]], file = tmp_file, format = format, paginate = paginate, lpp = lpp, ...) + tmp_file + }) + + zip(zipfile = file, files = unlist(tmp_files), flags = "-j") # -j: junk paths +} + export_table_raw <- function(x) { html_content <- gt::as_raw_html(x) html_parsed <- rvest::read_html(html_content) @@ -372,7 +425,7 @@ type_download_srv_table <- function(id, table_reactive) { output$data_download <- downloadHandler( filename = function() { - paste0(input$file_name, input$file_format) + file_download_format(table_reactive(), paste0(input$file_name, input$file_format)) }, content = function(file) { export_table( From 2f20ea0157f798c73af86cec481edf6f33a7e40d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 14 Jul 2026 08:57:31 +0100 Subject: [PATCH 02/10] docs: update NEWS --- NEWS.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/NEWS.md b/NEWS.md index dc78ce95..ac541f8d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,9 @@ # teal.widgets 0.6.1 +### Enhancements + +* Added support for `tbl_split` object from `gtsummary` package to `table_with_settings`. + ### Bug fixes * Fix snapshot failure during R CMD Check (#341). * Fixed problem with resize plot popup that does not render content with newer version of shiny (#350). From 407ae0393eaedc0179cb1ccc8a286055ebd4aebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 14 Jul 2026 08:59:19 +0100 Subject: [PATCH 03/10] fix: add utils prefix --- R/table_with_settings.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/table_with_settings.R b/R/table_with_settings.R index 2effba82..03024862 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -198,7 +198,7 @@ export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL tmp_file }) - zip(zipfile = file, files = unlist(tmp_files), flags = "-j") # -j: junk paths + utils::zip(zipfile = file, files = unlist(tmp_files), flags = "-j") # -j: junk paths } export_table_raw <- function(x) { From 908264d28c34708a4ae4485e9de6f73441fa3f71 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 08:02:27 +0000 Subject: [PATCH 04/10] [skip style] [skip vbump] Restyle files --- R/table_with_settings.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/table_with_settings.R b/R/table_with_settings.R index 03024862..4aa368e0 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -185,7 +185,7 @@ export_table.gt_tbl <- function(x, file, format, paginate = FALSE, lpp = NULL, . #' @keywords internal #' @exportS3Method export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL, ...) { - ext <- format # ".pdf" or ".txt" + ext <- format # ".pdf" or ".txt" tmp_dir <- tempfile() dir.create(tmp_dir) on.exit(unlink(tmp_dir, recursive = TRUE)) @@ -198,7 +198,7 @@ export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL tmp_file }) - utils::zip(zipfile = file, files = unlist(tmp_files), flags = "-j") # -j: junk paths + utils::zip(zipfile = file, files = unlist(tmp_files), flags = "-j") # -j: junk paths } export_table_raw <- function(x) { From 080bc7dee949cc8e509ec2efef46e14a6a3f6caa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 14 Jul 2026 09:03:01 +0100 Subject: [PATCH 05/10] fix: rcmd warnings --- R/table_with_settings.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/table_with_settings.R b/R/table_with_settings.R index 4aa368e0..beb8f289 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -87,8 +87,8 @@ render_table_to_html.tbl_split <- function(x, ...) { tables <- lapply(seq_along(x), function(tbl) { label <- attr(x[[tbl]], "variable_level", exact = TRUE) htmltools::tags$div( - if (!rlang::is_empty(label)) htmltools::tags$h4("Variable level:", label), - teal.widgets:::render_table_to_html(x[[tbl]]) + if (checkmate::test_string(label)) htmltools::tags$h4("Variable level:", label), + render_table_to_html(x[[tbl]]) ) }) htmltools::tags$div(tables) From 92270c2f466a9bbc2dfff2050a7db9c978aa42ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 14 Jul 2026 13:15:46 +0100 Subject: [PATCH 06/10] fix: improved filenmaes inside zips --- R/table_with_settings.R | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/R/table_with_settings.R b/R/table_with_settings.R index beb8f289..c79011e5 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -184,15 +184,21 @@ export_table.gt_tbl <- function(x, file, format, paginate = FALSE, lpp = NULL, . #' @method export_table tbl_split #' @keywords internal #' @exportS3Method -export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL, ...) { +export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL, file_name = file, ...) { ext <- format # ".pdf" or ".txt" tmp_dir <- tempfile() dir.create(tmp_dir) on.exit(unlink(tmp_dir, recursive = TRUE)) - base_name <- tools::file_path_sans_ext(basename(file)) + base_name <- tools::file_path_sans_ext(basename(file_name)) tmp_files <- lapply(seq_along(x), function(i) { + label <- attr(x[[i]], "variable_level", exact = TRUE) + if (checkmate::test_string(label)) { + base_name <- paste0(base_name, "_", i, "_", gsub("[^[:alnum:]]", "_", label)) + } else { + base_name <- paste0(base_name, "_", i) + } tmp_file <- file.path(tmp_dir, paste0(base_name, "_", i, ext)) export_table(x[[i]], file = tmp_file, format = format, paginate = paginate, lpp = lpp, ...) tmp_file @@ -433,7 +439,8 @@ type_download_srv_table <- function(id, table_reactive) { file = file, format = input$file_format, paginate = input$pagination_switch, - lpp = if (input$pagination_switch) as.numeric(input$lpp) + lpp = if (input$pagination_switch) as.numeric(input$lpp), + file_name = input$file_name ) } ) From 8685f30a6f6f85d0617b4854178210bc356b8979 Mon Sep 17 00:00:00 2001 From: github-actions <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 12:17:50 +0000 Subject: [PATCH 07/10] [skip style] [skip vbump] Restyle files --- R/table_with_settings.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/table_with_settings.R b/R/table_with_settings.R index c79011e5..046cdce0 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -184,7 +184,7 @@ export_table.gt_tbl <- function(x, file, format, paginate = FALSE, lpp = NULL, . #' @method export_table tbl_split #' @keywords internal #' @exportS3Method -export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL, file_name = file, ...) { +export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL, file_name = file, ...) { ext <- format # ".pdf" or ".txt" tmp_dir <- tempfile() dir.create(tmp_dir) From 4e661ab61de533686c31e760caef9877f1a972cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 14 Jul 2026 15:32:47 +0100 Subject: [PATCH 08/10] fix: better naming and fixes old bug --- R/table_with_settings.R | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/R/table_with_settings.R b/R/table_with_settings.R index 046cdce0..921bbe12 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -193,13 +193,13 @@ export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL base_name <- tools::file_path_sans_ext(basename(file_name)) tmp_files <- lapply(seq_along(x), function(i) { - label <- attr(x[[i]], "variable_level", exact = TRUE) + label <- as.vector(attr(x[[i]], "variable_level", exact = TRUE)) if (checkmate::test_string(label)) { - base_name <- paste0(base_name, "_", i, "_", gsub("[^[:alnum:]]", "_", label)) + base_name <- paste0(base_name, "_", gsub("[^[:alnum:]]", "_", label), "_", i) } else { base_name <- paste0(base_name, "_", i) } - tmp_file <- file.path(tmp_dir, paste0(base_name, "_", i, ext)) + tmp_file <- file.path(tmp_dir, base_name) export_table(x[[i]], file = tmp_file, format = format, paginate = paginate, lpp = lpp, ...) tmp_file }) @@ -214,7 +214,9 @@ export_table_raw <- function(x) { # xml_remove modifies the object so no need to ovewrite xml2::xml_remove(rvest::html_nodes(html_parsed, "caption, .gt_heading")) - rvest::html_table(html_parsed, fill = TRUE)[[1]] + tbl <- rvest::html_table(html_parsed, fill = TRUE)[[1]] + names(tbl) <- gsub("[\n\r\t]", " ", names(tbl)) + tbl } #' @name table_with_settings From e6492507b6bfaf18c64403cbbb7c9978d77c8e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 14 Jul 2026 15:34:29 +0100 Subject: [PATCH 09/10] fix: re-adds extension --- R/table_with_settings.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/table_with_settings.R b/R/table_with_settings.R index 921bbe12..3aa6fe53 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -199,7 +199,7 @@ export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL } else { base_name <- paste0(base_name, "_", i) } - tmp_file <- file.path(tmp_dir, base_name) + tmp_file <- file.path(tmp_dir, base_name, ext) export_table(x[[i]], file = tmp_file, format = format, paginate = paginate, lpp = lpp, ...) tmp_file }) From 406165ac88a0b8e97ab9b5097c874203628b49ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ver=C3=ADssimo?= <211358+averissimo@users.noreply.github.com> Date: Tue, 14 Jul 2026 15:35:34 +0100 Subject: [PATCH 10/10] fix: re-adds extension --- R/table_with_settings.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/table_with_settings.R b/R/table_with_settings.R index 3aa6fe53..c5093806 100644 --- a/R/table_with_settings.R +++ b/R/table_with_settings.R @@ -199,7 +199,7 @@ export_table.tbl_split <- function(x, file, format, paginate = FALSE, lpp = NULL } else { base_name <- paste0(base_name, "_", i) } - tmp_file <- file.path(tmp_dir, base_name, ext) + tmp_file <- file.path(tmp_dir, paste0(base_name, ext)) export_table(x[[i]], file = tmp_file, format = format, paginate = paginate, lpp = lpp, ...) tmp_file })