我刚开始使用该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()
Run Code Online (Sandbox Code Playgroud)
将观星结果保存到对象:
res <- stargazer(my.lm, float=FALSE)
Run Code Online (Sandbox Code Playgroud)
如果您看一下的内容,res那么您将看到它只是一系列文本行。cat()像这样将其写入文件
cat(res, file="tab_results.tex", sep="\n")
Run Code Online (Sandbox Code Playgroud)
该sep="\n"只需要因为文本在res行对象不要包含任何换行符自己。如果我们保留使用默认值,sep=" "那么您的表将作为一长行写入tex文件。
希望这可以帮助。