在 Rmarkdown html 输出中居中 Xtable

Centering a Xtable in Rmarkdown html output

我正在使用 xtablermarkdown 的 HTML 文档中创建 table,我遇到的问题是输出 table向左对齐,我需要它居中。我尝试使用 kable 包,但 table 太宽了。问题是是否有办法在 HTML 文档中将 tables 居中,类似于 fig.aling = 'center' 但对于 tables 不需要 LaTex .

Formatting html table in R 获得灵感,您可以使用 print.xtablehtml.table.attributes 参数添加 table 属性。

例如:

```{r, results='asis'}

library(xtable)

print(
   xtable(mtcars[1:2, 1:4], align="lcccc"), # align columns
   type = "html", 
   html.table.attributes = 'align="center", # center table in page
                            rules="rows",   # only keep horizontal rows
                            width=50%,      # increase table width to 50% page
                            frame="below"') # remove border except bottom rule

```