Stargazer options: resizebox and label
Asked Answered
W

3

5

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.

Wigfall answered 22/2, 2015 at 19:42 Comment(4)
Did anybody find a solution yet? Facing the same problem.Rotz
I was never able to find a better solution.Wigfall
Would you be willing to connect with me on LinkedIn linkedin.com/in/scott-boston? Thanks for you help again with that geopandas question.Coeternity
@Scott sure, just didWigfall
A
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.

Auston answered 30/11, 2016 at 18:14 Comment(3)
This is very similar to Nicolas' approach. Do you find that there are benefits to one versus the other?Wigfall
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.Auston
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)Openmouthed
C
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.

Cauchy answered 15/3, 2016 at 17:33 Comment(2)
Thanks @Cauchy for this smart answer. Little question though, how do we export the cat(table) file to a latex file ?Mat
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: <<table-code, echo=FALSE, cache=FALSE>>= cat(table) @Cauchy
M
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")
Micron answered 11/10, 2015 at 7:36 Comment(0)
A
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.

Auston answered 30/11, 2016 at 18:14 Comment(3)
This is very similar to Nicolas' approach. Do you find that there are benefits to one versus the other?Wigfall
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.Auston
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)Openmouthed

© 2022 - 2024 — McMap. All rights reserved.