Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Stargazer HTML table width?

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"))

Latex rendering

HTML rendering

like image 662
TDP Avatar asked Sep 16 '16 17:09

TDP


People also ask

How do you make a stargazer table smaller?

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.

How do I resize a column width in HTML?

Use the style attribute with the width or height properties to specify the size of a table, row or column.

How do you change the width of a table in HTML?

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.


2 Answers

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

enter image description here

like image 68
rawr Avatar answered Oct 01 '22 06:10

rawr


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">

like image 43
Nick Becker Avatar answered Oct 01 '22 05:10

Nick Becker