Skip to content

Instantly share code, notes, and snippets.

@moritzpschwarz
Last active August 10, 2020 22:37
Show Gist options
  • Save moritzpschwarz/e591ef53da4d4586b5c30f94961bfd82 to your computer and use it in GitHub Desktop.
Save moritzpschwarz/e591ef53da4d4586b5c30f94961bfd82 to your computer and use it in GitHub Desktop.
The `stargazer` arguments can be put inside of this function. Then the output for the PDF is resized e.g. through something like this: `resizebox.stargazer(df,summary = TRUE,header = FALSE,tab.width = "0.9\\textwidth")` Taken from https://www.thetopsites.net/article/54022279.shtml
# Original source: https://www.thetopsites.net/article/54022279.shtml
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")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment