2014-01-16 22 views
5

我刚开始使用stargazer包,使R中回归表,但无法弄清楚如何写表输出到.tex文件没有无论是浮动或文档环境(以及文档环境中的序言)。也就是说,我只是想要表格环境。我的工作流程是将表格浮动环境 - 以及相关的标题和标签 - 保存在纸张的正文中,并链接到表格的表格环境\input{}省略浮动和文档环境

这可能吗?

# 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() 

回答

4

保存您的占星结果的对象:

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

如果你看一看的res的内容,然后你会看到它只是一系列的文本行。因为文字在res行对象不包含任何换行符自己写这篇文章使用cat()这样

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

sep="\n"文件时,才需要。如果我们使用默认的sep=" ",那么你的表将作为一个长行写入tex文件。

希望这会有所帮助。

+0

不错。感谢对象的教训。 –