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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With