I really like the styling of tables that the stargazer package uses. The tables render fine in pdf using Knitr and Rstudio. However when I try to knit my .Rmd into an html page, the tables end up squished together. Chunk option fig.width
does not help, nor does the stargazer option column.sep.width
. Is there any way to change the table width or is there another workflow to get pretty summary tables in html?
Reproducible example:
{r test, results = "asis"}
stargazer::stargazer(attitude,
type = "html",
digits = 2,
summary.stat = c("mean","sd","median","min", "max"))
To adjust table size with stargazer, you can change the font size font. size= , make the Stargazer single row single. row = TRUE and change the space between columns column.
Use the style attribute with the width or height properties to specify the size of a table, row or column.
To manipulate the height or width of an entire table, place the size attribute (either "WIDTH=" or "HEIGHT=") within the <TABLE> code. To manipulate individual cells, place the size attribute within the code for that cell.
I am heavily biased towards htmlTable::htmlTable
, but I will add this anyway. htmlTable
, as the name would suggest, is only for making tables, so all the bells and whistles of stargazer are not included, but this function has many options for customizing the output. As such you may need to do extra work to get the output you need to put into a table.
Similar to the other answer, you can use css to manipulate the style of the table. For example, you can pass css to css.cell
:
---
output: html_document
---
```{r test, results='asis', include=FALSE}
stargazer::stargazer(attitude,
type = "html",
digits = 2,
summary.stat = c("mean","sd","median","min", "max"))
```
```{r}
## apply a list of functions to a list or vector
f <- function(X, FUN, ...) {
fn <- as.character(match.call()$FUN)[-1]
out <- sapply(FUN, mapply, X, ...)
setNames(as.data.frame(out), fn)
}
(out <- round(f(attitude, list(mean, sd, median, min, max)), 2))
```
```{r, results='asis'}
library('htmlTable')
htmlTable(out, cgroup = 'Statistic', n.cgroup = 5, caption = 'Table 1: default')
htmlTable(out, cgroup = 'Statistic', n.cgroup = 5, caption = 'Table 1: padding',
## padding to cells: top side bottom
css.cell = 'padding: 0px 10px 0px;')
```
The following tables for no padding and extra padding on the sides
Try going into the actual HTML in the file after you create it. There will be a table
tag that establishes the style, width, and so on. For example, if the table
tag is <table style="text-align:center">
set the width manually as <table style="text-align:center" width="3000">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With