Omit floating and document environments from stargazer regression table output
Asked Answered
A

1

6

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()
Addam answered 16/1, 2014 at 11:7 Comment(0)
R
6

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.

Riobard answered 16/1, 2014 at 11:59 Comment(1)
Nice. Thanks for the lesson on objects.Addam

© 2022 - 2024 — McMap. All rights reserved.