Create PDF using Rhtml and iterate over data frame

谁说我不能喝 提交于 2020-01-06 20:24:46

问题


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?


回答1:


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

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}

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



来源:https://stackoverflow.com/questions/31817908/create-pdf-using-rhtml-and-iterate-over-data-frame

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!