0

Below is a code to put a stargazer table in R markdown. To do so, I used some code found in the documentation of the stargazer package and I customize it a little bit.

---
title: "test stargazer in r markdown"
author: ""
date: "18/09/2019"
header-includes:
   - \usepackage{dcolumn}
output: pdf_document
---


```{r results='asis', echo = FALSE, message = FALSE}
library(stargazer)
set.seed(5)
temp <- rnorm(500, mean = 80, sd = 12)
sales <- 2 + temp * 3
for (i in 1:length(sales)) {
if (temp[i]<75 | temp[i]>95) sales[i] <- sales[i] + rnorm(1, 0, 25)
else sales[i] <- sales[i] + rnorm(1, 0, 8)
}
female <- rnorm(500, mean = 0.5, sd = 0.01)
icecream <- as.data.frame(cbind(temp, sales, female))

reg.model <- lm(sales ~ temp + female, data = icecream)
library(sandwich)
cov <- vcovHC(reg.model, type = "HC")
robust.se <- sqrt(diag(cov))

stargazer(reg.model, reg.model, 
          se = list(NULL, robust.se), 
          column.labels = c("default", "robust"), 
          align = TRUE, 
          notes.align = "l",
          type = "latex", 
          header = FALSE,
          notes = "Tout ceci est un texte bidon qui a pour unique but de prendre de la place afin de voir comment les notes sont traitées par le package 'stargazer'."
)
```

The text in the notes argument is long and therefore increases the width of the table whereas I'd like the text to be confined (with multiple break lines if necessary) under the original (narrower) table. I don't think this is possible with the functions of the package (but I may be wrong), is there a tweak to make that possible?

First image is the obtained output, second image is the desired output: Obtained output (bad):

Desired output:

Edit: I would like a solution so that the lines are broken automatically, not manually

Mark
  • 7,785
  • 2
  • 14
  • 34
bretauv
  • 7,756
  • 2
  • 20
  • 57
  • If it's a styling issue you can add CSS tags to make your table columns a fixed with (wrapping the text automatically). Take a look [at this question](https://stackoverflow.com/questions/29291633/adding-custom-css-tags-to-an-rmarkdown-html-document). – jake2389 Sep 18 '19 at 21:41
  • @jake2389 I have no idea how to do it, I've never used CSS so far, can you give me more details? – bretauv Sep 18 '19 at 21:46
  • I'm not sure if I understood correctly but it looks like you have a formatting issue where you don't want the table cells to overflow because of the large text? This is a formatting issue, so CSS might help. It might help if you add screenshots of what the output looks like and preferably of what you want the output to look like and we can go from there. – jake2389 Sep 18 '19 at 21:48
  • edited my post with obtained and desired outputs – bretauv Sep 18 '19 at 22:03
  • Okay, got it now. Take a look at [this answer first](https://stackoverflow.com/questions/21720264/stargazer-notes-line-wrap) and see if that works. – jake2389 Sep 18 '19 at 22:47
  • This answer does not really do the job since you have to try many times to get a text that is not really justified. I would prefer a CSS-based solution (if possible with details since I never used CSS) until the author of the package implements this function – bretauv Sep 19 '19 at 06:58

0 Answers0