从R数据帧生成LaTeX输出 [英] Generating LaTeX output from R data frame
问题描述
我正在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>>
.请参见编织示例,例如,
The xtable
package has some examples of how to generate tables - see vignette. When you're writing a chunk, make sure you set the chunk to <<results=tex>>
. See Sweave example, for instance, this.
这就是我输出data.frame的方式.
This is how I would output a data.frame.
<<results=tex>>
xtable(my.data.frame)
@
原始结果看起来像这样:
And the raw result would looks something like this:
> 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
\begin{table}[ht]
\begin{center}
\begin{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输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!