14

I included resize.height=0.5,resize.width=0.5 in the code chunk, but still can't resize the table generated by stargazer. Can anyone tell me why?

My code chunk options look like this: echo=FALSE,warning=FALSE,results='asis',resize.height=0.5,resize.width=0.5}

The stargazer codes are like this:

stargazer(did.student,student.control.kmt,student.control.neu,student.control.dpp,header = FALSE,
          title="DD Model",
          covariate.labels = c("Treatment","group","Treatment*group"),
          dep.var.labels = "attitude",
          column.labels   = c("","party1","Independent","party2"),
          label = "DiD-students")

Would appreciate any help!

-- Forgot to mention - I'm using beamer with the table.

yuan-ning
  • 537
  • 1
  • 4
  • 12

5 Answers5

7

I kind of solve the problem myself:

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.sep.width = "1pt" in stargazer().

Though the link here suggests using print(stargazer(),scalebox='0.7'), it doesn't work for me perhaps because I'm using Markdown with Beamer, but I'm not sure. Would still love to have more contribution on this.

I was hoping for a more straightforward answer, but this works!

Community
  • 1
  • 1
yuan-ning
  • 537
  • 1
  • 4
  • 12
4

This comment on GitHub inspired me to implement \resizebox{} into stargazer(). You can use resizebox.stargazer() to specify the size of the table outputted from stargazer() with tab.width and/or tab.height arguments. To activate the function, you need to run the following code first:

resizebox.stargazer = function(..., tab.width = "!", tab.height = "!"
                               ){
  #Activate str_which() function:
  require(stringr) 

  #Extract the code returned from stargazer()
  res = capture.output(
    stargazer::stargazer(...)
    )

  #Render the arguments:
  tab.width = tab.width
  tab.height = tab.height

  #Attach "}" between \end{tabular} and \end{table}
  res = 
    prepend(res, "}", before = length(res))

  #Input \resizebox before \begin{tabular}
  res = 
    c(res[1:str_which(res, "^\\\\begin\\{tabular\\}.*")-1],
      paste0("\\resizebox{",tab.width,"}{",tab.height,"}{%"),
      res[str_which(res, "^\\\\begin\\{tabular\\}.*"):length(res)]
      )

  #Produce the whole strings
  cat(res, sep = "\n")
}

You can specify the table size by e.g. resizebox.stargazer(..., tab.width = "0.7\\textwidth"). Note that you have to write the TeX commands from \\ instead of \.

Carlos Luis Rivera
  • 3,108
  • 18
  • 45
  • Thanks! This is wonderful. However, it seems that the unmodified text goes into output file. E.g. defining `out = output.tex` does not yield a scaled table but the original one. – Antti Apr 29 '19 at 13:54
4

I would follow @yuan-ning and manipulate the options of stargazer. Try the following for PDF output of R markdown:

stargazer(model_1, model_2, model_3, model_4, model_5,
          type = 'latex',
 
          header=FALSE, # to get rid of r package output text

          single.row = TRUE, # to put coefficients and standard errors on same line

          no.space = TRUE, # to remove the spaces after each line of coefficients

          column.sep.width = "3pt", # to reduce column width

          font.size = "small" # to make font size smaller

)
ToWii
  • 590
  • 5
  • 8
1

Here is an alternative to Carlos' solution that writes the output to a LaTeX file:

mkTexTable <- function(..., file){

    tbl <- capture.output({
        stargazer(...)
    })    

    tbl <- gsub("\\begin{tabular}", "\\resizebox{\\textwidth}{!}{\\begin{tabular}", tbl, fixed = T)
    tbl <- gsub("\\end{tabular}", "\\end{tabular}}", tbl, fixed = T)

    fileConn <- file(file)
    writeLines(tbl, fileConn)
    close(fileConn)
}

mkTexTable(lm1, lm2, "texOutput.tex")

This post also provided some help: https://stackoverflow.com/a/36018251/2289444

Antti
  • 1,263
  • 2
  • 16
  • 28
0

If the problem is with html Rmd files, you sould specify {r, results = 'asis'} at the beginning of the chunck and then in stargazer type = 'html'. That worked for me.