AndriSignorell/DescTools

PercTable in Rmarkdown as HTML?

jarbet opened this issue · 7 comments

jarbet commented

Rather than print a PercTable object, is there a way to render it as HTML in Rmarkdown?

For example, suppose I want to render this table as HTML in RMarkdown:

library(DescTools)

tab <- table(driver=d.pizza$driver, area=d.pizza$area)
PercTable(tab=tab, col.vars=2, margins=c(1,2))
#>                                                       
#>             area   Brent   Camden   Westminster    Sum
#> driver                                                
#>                                                       
#> Butcher     freq      72        1            22     95
#>             perc    6.0%     0.1%          1.8%   8.0%
#>                                                       
#> Carpenter   freq      29       19           221    269
#>             perc    2.4%     1.6%         18.5%  22.5%
#>                                                       
#> Carter      freq     177       47             5    229
#>             perc   14.8%     3.9%          0.4%  19.2%
#>                                                       
#> Farmer      freq      19       87            11    117
#>             perc    1.6%     7.3%          0.9%   9.8%
#>                                                       
#> Hunter      freq     128        4            24    156
#>             perc   10.7%     0.3%          2.0%  13.1%
#>                                                       
#> Miller      freq       6       41            77    124
#>             perc    0.5%     3.4%          6.4%  10.4%
#>                                                       
#> Taylor      freq      42      142            20    204
#>             perc    3.5%    11.9%          1.7%  17.1%
#>                                                       
#> Sum         freq     473      341           380  1'194
#>             perc   39.6%    28.6%         31.8% 100.0%
#> 

Created on 2023-04-11 by the reprex package (v2.0.1)

@jarbet, does |> with(pander::pander(ftab)) solve your issue? You need pander package to be installed.

Print-screen from rendered Quarto (newer generation or Rmd) document:
image

For more details on why this solution works, you may investigate the following results:

your_table |> names()
your_table |> with(ftab) # your_table$ftab
your_table |> with(ftab) |> class()
jarbet commented

@jarbet, does |> with(pander::pander(ftab)) solve your issue? You need pander package to be installed.

Yes, pander works for me, although it seems less customizable than other table packages (e.g. kableExtra, flextable).

For example, I can't figure out how to add a vertical line to the left of area column, or to add lines to separate the Sum column and row (e.g. virtical line to left of Sum column, horizontal line above Sum row)

Also, how did you alternative the color of your rows? My table has an all white background when knitting to HTML in Rmarkdown:

tab <- table(driver = d.pizza$driver, area = d.pizza$area)
tab <- PercTable(tab, col.vars = 2, margins = c(1,2))
pander(tab$ftab)

image

jarbet commented

pander creates a multi-row Markdown table that then is rendered to any other format by pandoc (in your case, to HTML). So for HTML, try a custom CSS code. You may create a CSS code block at the begining of your Rmd/qmd file and write your CSS code there or create a separate a CSS file. I usualy create a hidden CSS block, when a single HTML document needs customization. Website design is out of scope for DescTools. But striped tables are very common, so you can easily find more information on that elsewhere. If you like a more detailed answer, open the same question on StackOverflow and add link here. 2023 bal. 12, tr 03:37, Jaron Arbet @.***> rašė:

Sounds good. Thanks for your prompt replies in addressing both my issues!

I used Quarto, not R Markdown. I think, in Quarto table is striped by default.

jarbet commented

I used Quarto, not R Markdown. I think, in Quarto table is striped by default.

Interesting. I have been meaning to switch over to Quarto, especially for the presentations. The Revealjs format has a lot of cool features.

This gives me 1 more reason to switch though lol.

@jarbet, if your issue is solved, please, close this thread.