问题描述
我在 Ubuntu 上运行 R v2.14.1.我正在编写一个脚本,它将生成一个数据框,它代表一个结果表.
I am running R v2.14.1 on Ubuntu. I am writing a script that will generate a data frame, which represents a table of results.
我想将此表"输出为 .tex 文件,以便创建学术出版物"质量表以供打印.我听说过 Sweave(并阅读了一些关于 Sweave 的概述文档)——所以我认为这是继续前进的方式.然而,话虽如此,我实际上并没有看到 Sweave 将数据帧输出为 tex 文件的示例——到目前为止,我看到的所有 Sweave 示例似乎都是人为的,而不是我可以构建的东西.
I would like to output this 'table' as a .tex file so that I can create an 'academic publication' quality table, for printing. I have heard of Sweave (and read some overview docs about Sweave) - so I think this is the way to proceeed. However having said that I have not actually seen an example where Sweave outputs a dataframe as a tex file - all of the Sweave examples I have seen so far, seem contrived and not something that I can build upon.
我可以遵循一些准则来从数据框中输出 tex 吗?另外,如果我直接从我的 R 脚本构建 TeX 字符串并将字符串保存到文件中,它会更简单(更直接)吗?(我不清楚 Sweave 提供了哪些手动"手动构建 TeX 字符串).
Are there some guidelines I can follow, to output tex from a dataframe? Also, will it be simpler (more straight forward), if I built the TeX string directly from my R script and saved the string to file? (It is not clear to me, what Sweave offers over and above manually building the TeX string 'manually').
推荐答案
xtable
包有一些关于如何生成表格的示例 - 请参阅小插图.当您编写一个块时,请确保将块设置为 <<results=tex>>
.参见 Sweave 示例,例如 这个.
这就是我输出 data.frame 的方式.
This is how I would output a data.frame.
<<results=tex>>
xtable(my.data.frame)
@
原始结果如下所示:
> xtable(my.data.frame)
% latex table generated in R 2.14.1 by xtable 1.6-0 package
% Tue Feb 14 10:03:03 2012
egin{table}[ht]
egin{center}
egin{tabular}{rllr}
hline
& p & q & r \
hline
1 & condition\_a & grp\_1 & 3 \
2 & condition\_a & grp\_1 & 3 \
3 & condition\_a & grp\_1 & 4 \
4 & condition\_a & grp\_1 & 1 \
5 & condition\_b & grp\_1 & 4 \
6 & condition\_b & grp\_1 & 3 \
7 & condition\_b & grp\_1 & 5 \
8 & condition\_b & grp\_1 & 5 \
9 & condition\_a & grp\_2 & 4 \
10 & condition\_a & grp\_2 & 1 \
11 & condition\_a & grp\_2 & 1 \
12 & condition\_a & grp\_2 & 1 \
13 & condition\_b & grp\_2 & 5 \
14 & condition\_b & grp\_2 & 1 \
15 & condition\_b & grp\_2 & 5 \
16 & condition\_b & grp\_2 & 2 \
hline
end{tabular}
end{center}
end{table}
这篇关于从 R 数据帧生成 LaTeX 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!