diff --git a/NEWS.md b/NEWS.md index 79f50b67..859f0acb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -26,6 +26,10 @@ * `tableby()`: added an error if reserved terms are used in the by-variable. (#277) +* Fixed a bug to conform with new `knitr` publication. + +* Added an option for HTML footnotes (and superscripts) in `summary.tableby()`. (#298) + # arsenal v3.4.0 * Added a `pkgdown` site: https://eheinzen.github.io/arsenal/ diff --git a/R/summary.tableby.R b/R/summary.tableby.R index e0a3e403..10e6a7f0 100644 --- a/R/summary.tableby.R +++ b/R/summary.tableby.R @@ -18,7 +18,8 @@ #' the latter avoiding all formatting. #' \code{"html"} uses the HTML tag \code{} instead of the markdown formatting, and \code{"latex"} uses #' the LaTeX command \code{\\textbf}. -#' @param pfootnote Logical, denoting whether to put footnotes describing the tests used to generate the p-values. +#' @param pfootnote Logical, denoting whether to put footnotes describing the tests used to generate the p-values. Alternatively, +#' "html" to surround the outputted footnotes with \code{
  • }. #' @param term.name A character vector denoting the column name for the "terms" column. It should be the same length #' as the number of tables or less (it will get recycled if needed). The special value \code{TRUE} will #' use the y-variable's label for each table. @@ -97,11 +98,13 @@ as_data_frame_summary_tableby <- function(df, totals, hasStrata, term.name, cont if(!is.null(df$p.value)) df$p.value[grepl("^\\s*NA$", df$p.value) | is.na(df$p.value)] <- "" tests.used <- NULL - if(control$test && pfootnote) + if(control$test && (isTRUE(pfootnote) || identical(pfootnote, "html"))) { tests.used <- unique(df$test[df$test != "No test"]) - df$p.value <- ifelse(df$p.value == "", "", paste0(df$p.value, "^", as.integer(factor(df[["test"]], levels = tests.used)), "^")) - tests.used <- paste0(seq_along(tests.used), ". ", tests.used) + sup <- if(!is.null(text) && identical(text, "html")) c("", "") else if(isTRUE(text)) c(" (", ")") else c("^", "^") + + df$p.value <- ifelse(df$p.value == "", "", paste0(df$p.value, sup[1], as.integer(factor(df[["test"]], levels = tests.used)), sup[2])) + tests.used <- if(identical(pfootnote, "html")) c("
      ", paste0("
    1. ", tests.used, "
    2. "), "
    ") else paste0(seq_along(tests.used), ". ", tests.used) } #### don't show the same statistics more than once #### diff --git a/tests/testthat/test_lhs_tableby.R b/tests/testthat/test_lhs_tableby.R index ea01165c..de768c75 100644 --- a/tests/testthat/test_lhs_tableby.R +++ b/tests/testthat/test_lhs_tableby.R @@ -360,22 +360,22 @@ test_that("01/31/2019: modpval.tableby (#174, #175)", { tmp <- modpval.tableby(tmp, data.frame(y = "sex", strata = c("1", "2"), x = c("age", "arm"), p = c(1, 0.5)), use.pname = TRUE) expect_identical( capture.kable(summary(tmp, pfootnote = TRUE, text = TRUE)), - c("|fu.stat | | Male (N=916) | Female (N=583) | Total (N=1499) | p |", - "|:-------|:-------------|:---------------:|:---------------:|:---------------:|:--------:|", - "|1 |Age in Years | | | | 1.000^1^ |", - "| |- Mean (SD) | 58.253 (12.048) | 61.018 (10.649) | 59.336 (11.561) | |", - "| |- Range | 32.000 - 85.000 | 35.000 - 80.000 | 32.000 - 85.000 | |", - "| |Treatment Arm | | | | |", - "| |- A: IFL | 14 (16.1%) | 4 (7.1%) | 18 (12.6%) | |", - "| |- F: FOLFOX | 56 (64.4%) | 43 (76.8%) | 99 (69.2%) | |", - "| |- G: IROX | 17 (19.5%) | 9 (16.1%) | 26 (18.2%) | |", - "|2 |Age in Years | | | | |", - "| |- Mean (SD) | 60.686 (11.278) | 59.059 (11.824) | 60.054 (11.516) | |", - "| |- Range | 19.000 - 88.000 | 22.000 - 88.000 | 19.000 - 88.000 | |", - "| |Treatment Arm | | | | 0.500^1^ |", - "| |- A: IFL | 263 (31.7%) | 147 (27.9%) | 410 (30.2%) | |", - "| |- F: FOLFOX | 355 (42.8%) | 237 (45.0%) | 592 (43.7%) | |", - "| |- G: IROX | 211 (25.5%) | 143 (27.1%) | 354 (26.1%) | |", + c("|fu.stat | | Male (N=916) | Female (N=583) | Total (N=1499) | p |", + "|:-------|:-------------|:---------------:|:---------------:|:---------------:|:---------:|", + "|1 |Age in Years | | | | 1.000 (1) |", + "| |- Mean (SD) | 58.253 (12.048) | 61.018 (10.649) | 59.336 (11.561) | |", + "| |- Range | 32.000 - 85.000 | 35.000 - 80.000 | 32.000 - 85.000 | |", + "| |Treatment Arm | | | | |", + "| |- A: IFL | 14 (16.1%) | 4 (7.1%) | 18 (12.6%) | |", + "| |- F: FOLFOX | 56 (64.4%) | 43 (76.8%) | 99 (69.2%) | |", + "| |- G: IROX | 17 (19.5%) | 9 (16.1%) | 26 (18.2%) | |", + "|2 |Age in Years | | | | |", + "| |- Mean (SD) | 60.686 (11.278) | 59.059 (11.824) | 60.054 (11.516) | |", + "| |- Range | 19.000 - 88.000 | 22.000 - 88.000 | 19.000 - 88.000 | |", + "| |Treatment Arm | | | | 0.500 (1) |", + "| |- A: IFL | 263 (31.7%) | 147 (27.9%) | 410 (30.2%) | |", + "| |- F: FOLFOX | 355 (42.8%) | 237 (45.0%) | 592 (43.7%) | |", + "| |- G: IROX | 211 (25.5%) | 143 (27.1%) | 354 (26.1%) | |", "1. Modified by user" ) ) diff --git a/tests/testthat/test_tableby.R b/tests/testthat/test_tableby.R index 0b53eeb2..eb03cdd0 100644 --- a/tests/testthat/test_tableby.R +++ b/tests/testthat/test_tableby.R @@ -1479,3 +1479,78 @@ test_that("Warn if reserved word is used in tableby by-variable (#277)", { ) }) + +test_that("HTML footnotes (#298)", { + expect_identical( + capture.output(summary(tableby(sex ~ age + arm, data = mockstudy), text = "html", pfootnote = "html")), + c("" , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + "" , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " ", + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + " " , + "" , + "
    Male (N=916) Female (N=583) Total (N=1499) p value
    Age in Years 0.0481
       Mean (SD) 60.455 (11.369) 59.247 (11.722) 59.985 (11.519)
       Range 19.000 - 88.000 22.000 - 88.000 19.000 - 88.000
    Treatment Arm 0.1902
       A: IFL 277 (30.2%) 151 (25.9%) 428 (28.6%)
       F: FOLFOX 411 (44.9%) 280 (48.0%) 691 (46.1%)
       G: IROX 228 (24.9%) 152 (26.1%) 380 (25.4%)
    " , + "
      " , + "
    1. Linear Model ANOVA
    2. " , + "
    3. Pearson's Chi-squared test
    4. " , + "
    " , + "" + ) + ) +}) +