Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
55 changes: 54 additions & 1 deletion R/table_with_settings.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
llrs-roche marked this conversation as resolved.
}

#' @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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 variable_level or such.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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 label is somehow dropped o missed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me check if there's unpushed code

image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now with better naming:

<original_name>_<variable_level>_<index>

The index is just in case to avoid repetitions, and _<variable_level> is hidden if it's not a string

image

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)
Expand Down Expand Up @@ -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(
Expand Down
Loading