关于 r:Centering a Xtable in Rmarkdown html output | 珊瑚贝

Centering a Xtable in Rmarkdown html output


我正在使用 xtable 在 rmarkdown 的 HTML 文档中创建一个表格,我遇到的问题是输出表格左对齐,我需要它居中。我尝试使用 kable 包,但桌子太宽了。问题是是否有办法在 HTML 文档中将表格居中,例如 fig.aling = ‘center’ 但对于不需要 LaTex 的表格。

  • 我确定会有一个选项,但我不知道它,但快速解决方法是添加一点 html:tab = capture.output(print(xtable(mtcars[1:2, 1:4]), type=”html”, only.contents=TRUE)) ; cat(c(‘<table align=”center”, border=1>’, tab, ‘</table>’), sep=”\
    “)
  • 实际上,可能有一种方法可以设置 html.table.attributes 参数: print(xtable(mtcars[1:2, 1:4]), type =”html”, html.table.attributes=list(‘align=”center”, border=1’ )) 。请参阅 tertra 的答案 stackoverflow.com/questions/20200082/formatting-html-table-i??n-r
  • @user20650 你的第二条评论很完美,如果你想把它作为答案,我会接受。谢谢


从 Formatting html table in R 中获得灵感,您可以使用 print.xtable 的 html.table.attributes 参数添加表格属性。

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
“`{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

“`


来源:https://www.codenong.com/45637019/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9035.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?