在这篇文章中,我将概述最常用的包和博客,其中包含从不太直接的对象生成latex表的代码。请随意添加任何我错过的,和/或给提示,提示和小技巧,如何用R生成漂亮的乳胶表。

包:

xtable : for standard tables of most simple objects. A nice gallery with examples can be found here. memisc : tool for management of survey data, contains some tools for latex tables of (basic) regression model estimates. Hmisc contains a function latex() that creates a tex file containing the object of choice. It is pretty flexible, and can also output longtable latex tables. There's a lot of info in the help file ?latex miscFuncs has a neat function 'latextable' that converts matrix data with mixed alphabetic and numeric entries into a LaTeX table and prints them to the console, so they can be copied and pasted into a LaTeX document. texreg package (JSS paper) converts statistical model output into LaTeX tables. Merges multiple models. Can cope with about 50 different model types, including network models and multilevel models (lme and lme4). reporttools package (JSS paper) is another option for descriptive statistics on continuous, categorical and date variables. tables package is perhaps the most general LaTeX table making package in R for descriptive statistics stargazer package makes nice comparative statistical model summary tables

博客和代码片段

Paul Johnson的outreg函数在Latex中为回归的输出提供了类似stata的表。这个很好用。 正如在前面的问题中所给出的,有一个代码片段用于为lme4对象调整memisc包。

相关问题:

R/LaTeX表创建包的建议 Rreport/LaTeX质量输出包 使用xtable对latex输出的表进行排序 有办法从lme4 mer模型拟合对象产生一个LaTeX表吗? R data.frame与堆叠指定标题乳胶输出与xtable 从R自动向latex快速添加表,使用公式语言提供非常灵活和有趣的语法


当前回答

taRifx包中的两个实用程序可以协同使用,以生成嵌套层次结构的多行表。

library(datasets)
library(taRifx)
library(xtable)

test.by <- bytable(ChickWeight$weight, list( ChickWeight$Chick, ChickWeight$Diet) )
colnames(test.by) <- c('Diet','Chick','Mean Weight')
print(latex.table.by(test.by), include.rownames = FALSE, include.colnames = TRUE, sanitize.text.function = force)
#   then add \usepackage{multirow} to the preamble of your LaTeX document
#   for longtable support, add ,tabular.environment='longtable' to the print command (plus add in ,floating=FALSE), then \usepackage{longtable} to the LaTeX preamble

其他回答

... 和技巧#3在Xtable中多行条目

生成更多数据

moredata<-data.frame(Nominal=c(1:5), n=rep(5,5), 
        MeanLinBias=signif(rnorm(5, mean=0, sd=10), digits=4), 
        LinCI=paste("(",signif(rnorm(5,mean=-2, sd=5), digits=4),
                ", ", signif(rnorm(5, mean=2, sd=5), digits=4),")",sep=""),
        MeanQuadBias=signif(rnorm(5, mean=0, sd=10), digits=4), 
        QuadCI=paste("(",signif(rnorm(5,mean=-2, sd=5), digits=4),
                ", ", signif(rnorm(5, mean=2, sd=5), digits=4),")",sep=""))

names(moredata)<-c("Nominal", "n","Linear Model \nBias","Linear \nCI", "Quadratic Model \nBias", "Quadratic \nCI")

现在生成我们的xtable,使用sanze函数用正确的Latex换行命令替换列名(包括双反斜杠,这样R就满意了)

<<label=multilinetable, results=tex, echo=FALSE>>=
foo<-xtable(moredata)
align(foo) <- c( rep('c',3),'p{1.8in}','p{2in}','p{1.8in}','p{2in}' )
print(foo, 
            floating=FALSE, 
            include.rownames=FALSE,
            sanitize.text.function = function(str) {
                str<-gsub("\n","\\\\", str, fixed=TRUE)

                return(str)
            }, 
            sanitize.colnames.function = function(str) {
                str<-c("Nominal", "n","\\centering Linear Model\\\\ \\% Bias","\\centering Linear \\\\ 95\\%CI", "\\centering Quadratic Model\\\\ \\%Bias", "\\centering Quadratic \\\\ 95\\%CI \\tabularnewline")
                return(str)
            })
@  

(虽然这并不完美,因为我们需要\tabularnewline来正确格式化表格,Xtable仍然放入一个最终的\,所以我们最终在表头下面有一个空行。)

观星包是另一个不错的选择。它支持来自许多常用函数和包(lm、glm、svyreg、survival、pscl、AER)以及来自zelig的对象。除了回归表,它还可以输出数据帧的汇总统计,或者直接输出数据帧的内容。

我想再提一下“酿造”包。您可以编写一个带有占位符的LaTeX模板文件,然后“酿造”它以创建一个.tex文件,以\include或\input到您的LaTeX中。喜欢的东西:

\begin{tabular}{l l}
A & <%= fit$A %> \\
B & <%= fit$B %> \\
\end{tabular}

brew语法还可以处理循环,因此可以为数据框架的每一行创建一个表行。

taRifx包中的两个实用程序可以协同使用,以生成嵌套层次结构的多行表。

library(datasets)
library(taRifx)
library(xtable)

test.by <- bytable(ChickWeight$weight, list( ChickWeight$Chick, ChickWeight$Diet) )
colnames(test.by) <- c('Diet','Chick','Mean Weight')
print(latex.table.by(test.by), include.rownames = FALSE, include.colnames = TRUE, sanitize.text.function = force)
#   then add \usepackage{multirow} to the preamble of your LaTeX document
#   for longtable support, add ,tabular.environment='longtable' to the print command (plus add in ,floating=FALSE), then \usepackage{longtable} to the LaTeX preamble

感谢Joris提出这个问题。希望它能成为一个社区维基。

在乳胶的booktabs包产生好看的表格。这是一篇关于如何使用xtable创建使用图书标签的乳胶表的博客文章

我还会将apsrtable包添加到组合中,因为它可以生成漂亮的回归表。

另一个想法:其中一些包(特别是memisc和apsrtable)允许简单地扩展代码,为不同的回归对象生成表。问题中显示的lme4 memisc代码就是这样一个例子。开始一个github存储库来收集这样的代码片段可能是有意义的,随着时间的推移,甚至可能将其添加到memisc包中。什么人吗?