2014-01-16 79 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

不錯。感謝對象的教訓。 –