-
Notifications
You must be signed in to change notification settings - Fork 53
Fix as_html newlines and add parameters to export_as_tsv
#899
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0a8708c
fix
Melkiades 723c393
docs
Melkiades 114b10e
adding tests and moving out as_html
Melkiades c18dede
fixed #289 too
Melkiades 15ae7b1
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] c398003
[skip style] [skip vbump] Restyle files
github-actions[bot] 66ee56e
fix url
Melkiades 1dbcfbf
Merge branch '898_fix_as_html_newlines_and_sep_tsv@main' of github.co…
Melkiades File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| context("Exporting to HTML") | ||
|
|
||
| test_that("as_html smoke test", { | ||
| tmpf <- tempfile(fileext = ".html") | ||
|
|
||
| tbl <- tt_to_export() | ||
| oldo <- options(viewer = identity) | ||
| expect_silent(fl <- Viewer(tbl)) | ||
| xml2::read_html(fl) | ||
| expect_true(TRUE) | ||
| options(oldo) | ||
| }) | ||
|
|
||
| test_that("as_html Viewer with newline test", { | ||
| tmpf <- tempfile(fileext = ".html") | ||
|
|
||
| colfuns <- list( | ||
| function(x) rcell(mean(x), format = "xx.x"), | ||
| function(x) rcell(sd(x), format = "xx.x") | ||
| ) | ||
| varlabs <- c("Mean Age", "SD\nLine Break!!! \nAge") | ||
|
|
||
| lyt <- basic_table() %>% | ||
| split_cols_by_multivar(c("AGE", "AGE"), varlabels = varlabs) %>% | ||
| analyze_colvars(afun = colfuns) | ||
|
|
||
| tbl_wrapping <- build_table(lyt, DM) | ||
|
|
||
| tbl_normal <- rtable( | ||
| header = c("Treatement\nN=100", "Comparison\nN=300"), | ||
| format = "xx (xx.xx%)", | ||
| rrow("A", c(104, .2), c(100, .4)), | ||
| rrow("B", c(23, .4), c(43, .5)), | ||
| rrow(), | ||
| rrow("this is a very long section header"), | ||
| rrow("estimate", rcell(55.23, "xx.xx", colspan = 2)), | ||
| rrow("95% CI", indent = 1, rcell(c(44.8, 67.4), format = "(xx.x, xx.x)", colspan = 2)) | ||
| ) | ||
| oldo <- options(viewer = identity) | ||
| expect_silent(fl <- Viewer(tbl_wrapping)) | ||
| expect_silent(fl <- Viewer(tbl_normal)) | ||
| xml2::read_html(fl) | ||
| expect_true(TRUE) | ||
| options(oldo) | ||
| }) | ||
|
|
||
| test_that("as_html does not trim whitespace", { | ||
| tbl <- rtable( | ||
| header = LETTERS[1:3], | ||
| format = "xx", | ||
| rrow(" r1", 1, 2, 3), | ||
| rrow(" r 2 ", 4, 3, 2, indent = 1), | ||
| rrow("r3 ", indent = 2) | ||
| ) | ||
| html_tbl <- as_html(tbl) | ||
| html_parts <- html_tbl$children[[1]][[2]]$children | ||
| expect_true(all(sapply(1:4, function(x) "white-space: pre;" %in% html_parts[[x]]$attribs))) | ||
| }) | ||
|
|
||
| test_that("as_html bolding works", { | ||
| tbl <- rtable( | ||
| header = LETTERS[1:3], | ||
| format = "xx", | ||
| rrow(" r1", 1, 2, 3), | ||
| rrow(" r 2 ", 4, 3, 2, indent = 1), | ||
| rrow("r3 ", indent = 2) | ||
| ) | ||
| html_tbl <- as_html(tbl, bold = "row_names") | ||
| html_parts <- html_tbl$children[[1]][[2]]$children | ||
| expect_true(all(sapply(2:4, function(x) "font-weight: bold;" %in% html_parts[[x]]$children[[1]][[1]]$attribs))) | ||
| }) | ||
|
|
||
| test_that("as_html header line works", { | ||
| tbl <- rtable( | ||
| header = LETTERS[1:3], | ||
| format = "xx", | ||
| rrow(" r1", 1, 2, 3), | ||
| rrow(" r 2 ", 4, 3, 2, indent = 1), | ||
| rrow("r3 ", indent = 2) | ||
| ) | ||
| html_tbl <- as_html(tbl, header_sep_line = TRUE) | ||
| html_parts <- html_tbl$children[[1]][[2]]$children[[1]]$children[[1]] | ||
| expect_true(all(sapply(1:4, function(x) "border-bottom: 1px solid black;" %in% html_parts[[x]]$attribs))) | ||
| }) | ||
|
|
||
| # https://github.com/insightsengineering/rtables/issues/872 | ||
| test_that("as_html indentation is translated to rows with linebreaks", { | ||
| lyt <- basic_table() %>% | ||
| split_cols_by("ARM") %>% | ||
| split_rows_by("SEX") %>% | ||
| analyze("AGE", afun = function(x) { | ||
| mn <- round(mean(x), 2) | ||
| if (!is.nan(mn) && mn > mean(DM$AGE)) { | ||
| val <- paste(mn, " ^ ", sep = "\n") | ||
| } else { | ||
| val <- paste(mn) | ||
| } | ||
| in_rows(my_row_label = rcell(val, | ||
| format = "xx" | ||
| )) | ||
| }) | ||
| tbl <- build_table(lyt, DM) | ||
|
|
||
| # Resolves correctly \n | ||
| expect_silent(res <- as_html(tbl, expand_newlines = TRUE)) | ||
| expect_equal( | ||
| as.character(res$children[[1]][[2]]$children[[7]]$children[[1]][[1]]), | ||
| '<td style="text-align: left; padding-left: 3ch;"></td>' | ||
| ) | ||
| expect_equal( | ||
| as.character(res$children[[1]][[2]]$children[[7]]$children[[1]][[2]]), | ||
| '<td style="text-align: center;"> ^ </td>' | ||
| ) | ||
| }) | ||
|
|
||
| test_that("as_html expands or not newlines depending on expand_newlines", { | ||
| # Table with both col/row names with newlines | ||
| iris_mod <- iris %>% | ||
| mutate(Species2 = as.factor(paste0("General", "\n ", as.character(Species)))) %>% | ||
| mutate(Species = as.factor(sample(paste0("Petal", "\n ", as.character(Species))))) | ||
|
|
||
| # Also the statistic has a newline | ||
| lyt <- basic_table() %>% | ||
| split_cols_by("Species") %>% | ||
| split_rows_by("Species2") %>% | ||
| analyze("Sepal.Length", afun = function(x) { | ||
| list( | ||
| "mean \n (sd)" = rcell(c(mean(x), sd(x)), format = "xx.xx (xx.xx)"), | ||
| "range" = diff(range(x)) | ||
| ) | ||
| }) | ||
|
|
||
| tbl <- build_table(lyt, iris_mod) | ||
| thtml <- as_html(tbl) | ||
| thtml_expanded_newlines <- as_html(tbl, expand_newlines = TRUE) | ||
|
|
||
| expect_true(grepl(as.character(thtml), pattern = "General\\n setosa")) | ||
| expect_false(grepl(as.character(thtml_expanded_newlines), pattern = "General\\n setosa")) # diff cells! | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.