examples/stackoverflow/59641155_reprex.md

The object returned by summary_table is a character matrix with the additional S3 class qwraps2_summary_table. The rowgroup names Value1 and Group are not part of the character matrix explicitly, they are part attributes. The print method for the qwraps2_summary_table object builds the table as need for the appropriate markup language, LaTeX or markdown.

Two edits the the example posted to get the table you are looking for:

  1. Add options(qwraps2_markup = "markdown") to your script. The default mark up language is LaTeX, setting this option changes the default to markdown.

  2. Do not wrap summary_table inside of knitr::kable: this prevents the needed print method from being called.

options(qwraps2_markup = "markdown")

set.seed(12345)
df <- data.frame(var1  = floor(runif(10,1000000,5000000)),
                 group = rep(c("A","B"),5),
                 event = rep(c("Yes","No"),5))


summary <- list("Value1" =
                list("min" = ~ min(var1),
                     "max" = ~ max(var1),
                     "mean (sd)" = ~ qwraps2::mean_sd(var1)),
                "Group" =
                  list("Yes" = ~ qwraps2::n_perc0(group == "A"),
                       "No"  = ~ qwraps2::n_perc0(group == "B")))


tab <- qwraps2::summary_table(df, summaries = summary, by = "event")
str(tab)
#>  'qwraps2_summary_table' chr [1:5, 1:2] "1665487" "4958947" ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : chr [1:5] "min" "max" "mean (sd)" "Yes" ...
#>   ..$ : chr [1:2] "No (N = 5)" "Yes (N = 5)"
#>  - attr(*, "rgroups")= Named int [1:2] 3 2
#>   ..- attr(*, "names")= chr [1:2] "Value1" "Group"

tab
#> 
#> 
#> |                       |No (N = 5)                         |Yes (N = 5)                      |
#> |:----------------------|:----------------------------------|:--------------------------------|
#> |**Value1**             |&nbsp;&nbsp;                       |&nbsp;&nbsp;                     |
#> |&nbsp;&nbsp; min       |1665487                            |2300381                          |
#> |&nbsp;&nbsp; max       |4958947                            |4043929                          |
#> |&nbsp;&nbsp; mean (sd) |3,741,784.20 &plusmn; 1,370,520.00 |3,392,933.80 &plusmn; 782,295.15 |
#> |**Group**              |&nbsp;&nbsp;                       |&nbsp;&nbsp;                     |
#> |&nbsp;&nbsp; Yes       |0 (0)                              |5 (100)                          |
#> |&nbsp;&nbsp; No        |5 (100)                            |0 (0)                            |

Created on 2020-09-15 by the reprex package (v0.3.0)

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       macOS Catalina 10.15.6      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Denver              
#>  date     2020-09-15                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
#>  backports     1.1.9   2020-08-24 [1] CRAN (R 4.0.2)
#>  callr         3.4.4   2020-09-07 [1] CRAN (R 4.0.2)
#>  cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.0)
#>  devtools      2.3.1   2020-07-21 [1] CRAN (R 4.0.2)
#>  digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.0)
#>  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.0)
#>  knitr         1.29    2020-06-23 [1] CRAN (R 4.0.0)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.0)
#>  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.0)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
#>  processx      3.4.4   2020-09-03 [1] CRAN (R 4.0.2)
#>  ps            1.3.4   2020-08-11 [1] CRAN (R 4.0.2)
#>  qwraps2       0.5.0   2020-09-14 [1] local         
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
#>  Rcpp          1.0.5   2020-07-06 [1] CRAN (R 4.0.0)
#>  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)
#>  rlang         0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
#>  rmarkdown     2.3     2020-06-18 [1] CRAN (R 4.0.0)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
#>  testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.0)
#>  usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.0)
#>  withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
#>  xfun          0.17    2020-09-09 [1] CRAN (R 4.0.2)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library


dewittpe/qwraps2 documentation built on Jan. 4, 2024, 1:59 p.m.