4

Is it possible to incorporate resizebox into stargazer in R? I'm trying to create a table that is too wide to fit, even on landscape perspective. I manually added resizebox{\textwidth}{!} { \begin{tabular} \end{tabular} } to the .tex file, and I like how it looks. However, I'd like for my .Rnw file to be complete, so that I can generate pdf perfectly without additional changes to the .tex file.

On a related note, stargazer causes pdflatex to output multiple warnings due to it including \label{} when no label is specified. These don't affect the pdf's creation, but they cause confusion when there are other errors present.

Again, I can manually delete these lines from the .tex file, or assign label names. However, I would like to simply tell stargazer not to include this line at all.

Thomas
  • 43,637
  • 12
  • 109
  • 140
Shffl
  • 396
  • 3
  • 18

3 Answers3

5

It's not ideal, but you can manually manipulate the LaTeX code output from stargazer with capture.output() and gsub().

table <- capture.output({ # Store the stargazer output in a string
  stargazer(iris, header=F) # e.g.
})
table <- gsub("\\begin{tabular}","\\resizebox{0.9\\textwidth}{!}{\\begin{tabular}", table,fixed=T)
table <- gsub("\\end{tabular}","\\end{tabular}}", table,fixed=T)
cat(table)

You can also extract that procedure into a method if you need it in more than one place.

Nicolas
  • 2,297
  • 3
  • 28
  • 40
  • Thanks @Nicolas for this smart answer. Little question though, how do we export the cat(table) file to a latex file ? – Jb_Eyd Dec 11 '19 at 11:32
  • depends on your setup, but you would put the `cat(table)` in the same place you would write `stargazer(...)`. e.g. in a Rnw file: `<>= cat(table) @` – Nicolas Dec 11 '19 at 15:11
1

To answer part 2 of your question, you can use label to label tables. This way you don't have to manually delete the empty \label{} from the .tex file. You will also be able to reference your tables using \ref{your.table.label}.

stargazer(df, title = "Statistical Summary", label="your.table.label", table.placement = "H")
user49017
  • 137
  • 1
  • 6
1

The best way I've found is to use set the stargazer option float to FALSE and then use cat() to manually put the scalebox in the float environment; for example:

<< results='asis', echo = FALSE>>=

cat("\\begin{table}[!htbp]")
cat("\\centering")
cat("\\caption{OLS Regression Results by Metal Level}")
cat("\\label{OLS}")
cat("\\scalebox{.8}{")

stargazer(models$model1OLS,
          float = FALSE)

cat("}") # for the end of the scalebox
cat("\\end{table}")

@

Note that you will also have to manually label, center, and caption the table. This will almost surely work with using resizebox instead of scalebox, but I haven't tried.

  • This is very similar to Nicolas' approach. Do you find that there are benefits to one versus the other? – Shffl Dec 01 '16 at 19:00
  • No real advantage or disadvantage, just slightly more readable (to me) code at the cost of more typing. I do a fair amount of collaboration with people who are very familiar with Latex, but not with R (most economists use Stata) and I've found this way easier to explain exactly what's going on. – Cameron Ellis Dec 04 '16 at 17:36
  • it's indeed more readable for R-people thus preferable. I wonder if stargazer will come in a future version with a rescale option (as all the other latex options such as positioning etc are implemented) – Fitzroy Hogsflesh May 29 '19 at 17:00