使用Rhtml创建PDF并遍历数据框 [英] Create PDF using Rhtml and iterate over data frame

查看:73
本文介绍了使用Rhtml创建PDF并遍历数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据帧,其中的人口统计信息分为16组.我基本上需要遍历这些组并为每个组创建一个PDF页面.我已经尝试过使用Rhtml,但到目前为止,我只能生成一页.有没有使用模板的方法?

I have a data frame that has demographic information split into 16 groups. I basically need to iterate over these groups and create a PDF page for each group. I've tried using Rhtml but so far I can only get one page to generate. Is there a way to use templates or something?

推荐答案

当需要PDF输出时,为什么不直接将.Rnw编译为.pdf?

When you need PDF output, why don't you directly compile .Rnw to .pdf?

这里是使用iris数据集的示例.它将每个物种的前几行打印在新页面上:

Here an example using the iris dataset. It prints the first few rows of each species on a new page:

\documentclass{article}
\begin{document}

<<results = "asis", echo = FALSE>>=
library(xtable)

newpage <- ""

invisible(lapply(unique(iris$Species), FUN = function(x) {

  cat(newpage)
  cat(sprintf("\\section{%s}", x))
  current <- head(subset(x = iris, subset = Species == x))
  print(xtable(current))
  newpage <<- "\\clearpage"
}))
@

\end{document}

我还使用了xtable来轻松获得格式正确的表.输出如下所示:

I additionally used xtable to easily get a nicely formatted table. The output looks like this:

这篇关于使用Rhtml创建PDF并遍历数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆