Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Omit floating and document environments from stargazer regression table output

Tags:

r

stargazer

I just started using the stargazer package to make regression tables in R, but can't figure out how to write table output to a .tex file without either floating or document environments (and preamble in the case of the document environment). That is, I just want the tabular environment. My work flow is to keep the table floating environment - and the associated captions and labels -- in the body of the paper and link to the table's tabular environment with \input{}.

Is this possible?

# bogus data
my.data <- data.frame(y = rnorm(10), x = rnorm(10))
my.lm <- lm(y ~ x, data=my.data)

# if I write to file, then I can omit the floating environment,
# but not the document environment
# (i.e., file contains `\documentclass{article}` etc.)
stargazer(my.lm, float=FALSE, out="option_one.tex")

# if I write to a text connection with `sink`,
# then I can omit both floating and document environments, 
# but not commands
# (i.e., log contains `sink` and `stargazer` commands)
con <- file("option_two.tex")
sink(con)
stargazer(my.lm, float=FALSE)
sink()
like image 284
Richard Herron Avatar asked Jan 16 '14 11:01

Richard Herron


1 Answers

Save your stargazer results to an object:

res <- stargazer(my.lm, float=FALSE)

If you take a look at the contents of res then you'll see it's just a series of lines of text. Write this to a file using cat() like this

cat(res, file="tab_results.tex", sep="\n")

The sep="\n" is only required because the lines of text in the res object dont contain any line breaks themselves. If we leave use the default sep=" " then your table will be written to the tex file as one long line.

Hope this helps.

like image 119
Stuples Avatar answered Sep 18 '22 21:09

Stuples