I'm using xtable
to create a table in a HTML document in rmarkdown
, the problem I have is that the output table is align to the left and I need it to be center. I tried with the kable
package but then the table is to wide. The question is if there is a way to center tables in an HTML document, something like fig.aling = 'center'
but for tables that doesn't require LaTex
.
Asked
Active
Viewed 1,837 times
1

Alejandro Andrade
- 2,196
- 21
- 40
-
Im sure there will be an option but i dont know it offhand, but a quick fix is to add a little html: `tab = capture.output(print(xtable(mtcars[1:2, 1:4]), type="html", only.contents=TRUE)) ; cat(c('
', tab, '
'), sep="\n")` – user20650 Aug 11 '17 at 14:18 -
actually, there may be a way setting `html.table.attributes` argument : `print(xtable(mtcars[1:2, 1:4]), type = "html", html.table.attributes=list('align="center", border=1' ))` . See tertra's answer https://stackoverflow.com/questions/20200082/formatting-html-table-in-r – user20650 Aug 11 '17 at 14:34
-
1@user20650 your second comment works perfect, if you want make it an answer and i'll accept it. Thanks – Alejandro Andrade Aug 11 '17 at 17:00
1 Answers
0
Taking inspiration from Formatting html table in R , you can add table attributes using the html.table.attributes
argument of print.xtable
.
So for example:
```{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
```

user20650
- 24,654
- 5
- 56
- 91