数据帧列表到pdf

时间:2012-03-14 10:01:54

标签: r pdf

我有一个函数可以生成不同维度的数据框列表。然后,我想创建一个pdf文件,按顺序显示此输出。

df1 <- data.frame(c(1,2,3,4,5),c("a","b","c","d","e"))
names(df1) <- c("no","letter")

df2 <- data.frame(c(1,2,3,4,5),c("a","b","c","d","e"),c("a","b","c","d","e"))
names(df2) <- c("no","letter1","letter2")

outputs <- list(df1,df2)

是否有适用于此的软件包,或者是否可以使用gdata软件包中的textplot等函数然后导出为pdf?

3 个答案:

答案 0 :(得分:3)

以下是使用Sweave的最小示例。

创建一个名为test.Rnw的文件,其中包含:

\documentclass[a4paper,11pt]{article}

\begin{document}

<<echo=F>>=
df1 <- data.frame(c(1,2,3,4,5),c("a","b","c","d","e"))
names(df1) <- c("no","letter")

df2 <- data.frame(c(1,2,3,4,5),c("a","b","c","d","e"),c("a","b","c","d","e"))
names(df2) <- c("no","letter1","letter2")

outputs <- list(df1,df2)

outputs
@

\end{document}

然后从命令行调用:

R CMD R Sweave test.Rnw
pdflatex test.Rnw

也许这会让你开始。

答案 1 :(得分:3)

一种可能的方法是绘制数据集的表(如果要避免使用TEX)。

library(gridExtra)

pdf(file="file_name.pdf", width=20)
    grid.newpage()
    grid.draw(tableGrob(head(iris, 10), name="test"))
dev.off()

enter image description here

与R一样,有一千种方法可以做。

plot(...)
text(...,xtable(...))

xtable函数也可用于改善 jmsigner 答案中的外观。

答案 2 :(得分:2)

包gridExtra提供grid.table(和tableGrob用于不立即打印),它们非常强大 http://cran.r-project.org/web/packages/gridExtra/index.html
以下是grid.table的一些示例:
http://rwiki.sciviews.org/doku.php?id=tips:graphics-grid:table

    library(gridExtra)

    df1 <- data.frame(c(1,2,3,4,5),c("a","b","c","d","e"))
    names(df1) <- c("no","letter")

    df2 <- data.frame(c(1,2,3,4,5),c("a","b","c","d","e"),c("a","b","c","d","e"))
    names(df2) <- c("no","letter1","letter2")

    outputs <- list(df1,df2)
    pdf(file="filename.pdf")
    plyr::l_ply(outputs, grid.table)
    dev.off()