-
-
Notifications
You must be signed in to change notification settings - Fork 8
Adds support for tbl_split to table_with_settings
#353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
0d29380
2f20ea0
407ae03
908264d
080bc7d
92270c2
8685f30
4e661ab
e649250
406165a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be a bit silly but the splits are also used to remove some columns. It would be great if instead of a file6de06d6041ec_1.csv and a file6de06d6041ec_2.csv we would get a file name a bit more meaningful: the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've tried by keeping original file name and adding the variable level when possible. Check it out on 92270c2
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It didn't work, now files are file112361464645_1_1.csv or such. My guess is that the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| export_table(x[[i]], file = tmp_file, format = format, paginate = paginate, lpp = lpp, ...) | ||
| tmp_file | ||
| }) | ||
|
|
||
| utils::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( | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.