Skip to content
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

Align table html output with ASCII output #780

Merged
merged 9 commits into from
Nov 14, 2023
16 changes: 12 additions & 4 deletions R/as_html.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ div_helper <- function(lst, class) {
#' @param class_th class for `th` tag
#' @param width width
#' @param link_label link anchor label (not including \code{tab:} prefix) for the table.
#' @param bold_main_title whether the main title should be in bold. Defaults to `FALSE`.
#'
#' @return A \code{shiny.tag} object representing \code{x} in HTML.
#'
Expand Down Expand Up @@ -61,7 +62,8 @@ as_html <- function(x,
class_tr = NULL,
class_td = NULL,
class_th = NULL,
link_label = NULL) {
link_label = NULL,
bold_main_title = FALSE) {
if (is.null(x)) {
return(tags$p("Empty Table"))
}
Expand Down Expand Up @@ -152,12 +154,14 @@ as_html <- function(x,
)
})

hsep_line <- tags$hr(class = "solid")

hdrtag <- div_helper(
class = "rtables-titles-block",
list(
div_helper(
class = "rtables-main-titles-block",
lapply(main_title(x), tags$p,
lapply(main_title(x), if (bold_main_title) tags$b else tags$p,
class = "rtables-main-title"
)
),
Expand All @@ -179,7 +183,7 @@ as_html <- function(x,
class = class_table,
tags$caption(sprintf("(\\#tag:%s)", link_label),
style = "caption-side:top;",
.noWS = "after-begin", hdrtag
.noWS = "after-begin"
)
)
)
Expand Down Expand Up @@ -210,10 +214,13 @@ as_html <- function(x,
## we want them to be there but empty??
ftrlst <- list(
if (length(mat$ref_footnotes) > 0) rfnotes,
if (length(mat$ref_footnotes) > 0) hsep_line,
if (length(main_footer(x)) > 0) mftr,
if (length(main_footer(x)) > 0 && length(prov_footer(x)) > 0) tags$br(),
if (length(prov_footer(x)) > 0) pftr
)

if (length(ftrlst) > 0) ftrlst <- c(list(hsep_line), ftrlst)
ftrlst <- ftrlst[!vapply(ftrlst, is.null, TRUE)]

ftrtag <- div_helper(
Expand All @@ -223,7 +230,8 @@ as_html <- function(x,

div_helper(
class = "rtables-all-parts-block",
list( # hdrtag,
list(
hdrtag,
tabletag,
ftrtag
)
Expand Down
5 changes: 4 additions & 1 deletion man/as_html.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/test-exporters.R
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ test_that("as_html does not trim whitespace", {
rrow("r3 ", indent = 2)
)
html_tbl <- as_html(tbl)
html_parts <- html_tbl$children[[1]][[1]]$children
html_parts <- html_tbl$children[[1]][[2]]$children
expect_true(all(sapply(1:4, function(x) html_parts[[x]]$attribs$style == "white-space:pre;")))
})

Expand Down