r - 使用 Brew、Pander 和 R 自动化 LaTeX 报告

标签 r latex pandoc xtable pander

我正在尝试编写一个brew模板,它接受一个数据集,并生成一个简单的数据字典。

我想要一个单独的页面,其中包含变量的名称,以及该变量的频率表,所以到目前为止我已经写了这个:

返回频率表的 R 函数:

#produce frequency table, in vector, out dataframe
procfreq<-function(x) {
  #find frequencies
  temp<-as.data.frame(table(x))
  #generate percents
  temp[,3]<-temp[,2]/sum(temp[,2])
  #name columns
  names(temp)<-c("Values","Frequencies","Percent")
  return(temp)
}

然后我在 Brew 循环中应用该函数:

  <% for (i in seq_along(names(testData))) { -%>
  \pagebreak
  <%= cat("\\section{",names(testData)[i],"}",sep="") %>
  <%= xtable(procfreq(testData[,i]),names(testData)[i],names(testData)[i]) %> 
  \clearpage
<% } -%>

我收到错误“在 cat(list(...),file,sep,fill,labels,append) 中: 参数 1(类型“list”)无法由“cat”处理

我知道这是来自 <%= xtable(procfreq(testData[,i]),names(testData)[i],names(testData)[i]) %>语句,如果我将其包装在 print(xtable(procfreq(testData[,i]),names(testData)[i],names(testData)[i])) 中,错误就会消失 然而,由于某种原因,我现在得到了每个表两次的 xtable LaTeX 输出,这是一个主要问题,因为手动删除额外的表违背了自动化报告的目的。

查看https://learnr.wordpress.com/2009/09/09/brew-creating-repetitive-reports/#X12后 我尝试使用 include_tbl 函数,它似乎使用了我已经使用的相同 print(xtable(...)) 概念,并且我遇到了与 xtable LaTeX 输出在每个循环中出现两次相同的问题。

自从这篇文章是 6 年前写的以来,我猜测从那时起 R 中的某些内容已经发生了变化,这影响了示例的功能。

据我所知,Brew 模板无法在不复制 R 表的情况下使用它们,这不可能是真的。

最后,我的 session 信息:

R version 3.2.1 (2015-06-18)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.4 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] xtable_1.7-4    rmarkdown_0.7   rapport_0.51    yaml_2.1.13     plyr_1.8.3      pander_0.5.2    markdown_0.7.7  lattice_0.20-33
 [9] knitr_1.10.5    ggplot2_1.0.1   foreign_0.8-65  brew_1.0-6     

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.0      reshape_0.8.5    digest_0.6.8     MASS_7.3-43      grid_3.2.1       gtable_0.1.2     magrittr_1.5     scales_0.2.5    
 [9] stringi_0.5-5    reshape2_1.4.1   proto_0.3-10     stringr_1.0.0    munsell_0.4.2    colorspace_1.2-6 htmltools_0.2.6 

最佳答案

我认为问题在于 xtable 的输出是用来打印的,而不是喂给猫的。这似乎有效,

procfreq<-function(x) {
  #find frequencies
  temp<-as.data.frame(table(x))
  #generate percents
  temp[,3]<-temp[,2]/sum(temp[,2])
  #name columns
  names(temp)<-c("Values","Frequencies","Percent")
  return(temp)
}

library(xtable)
<% for (i in seq_along(names(iris))) { -%>
  \pagebreak
<%= cat("\\section{",names(iris)[i],"}",sep="") %>
  <% print(xtable(procfreq(iris[,i]),names(iris)[i],names(iris)[i])) %> 
  \clearpage
<% } -%>

我想知道在 magrittr/brew 混淆之前可以删除多少空格。

关于r - 使用 Brew、Pander 和 R 自动化 LaTeX 报告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31757043/

相关文章:

latex - 将两张带标题的图像并排放置并在 Latex 中控制它们的高度

haskell - 如何在 Hakyll 中使用 Pandoc 过滤器?

markdown - pandoc-citeproc 如何对引文进行排序?

r - 如何检查 R 函数的输入参数是否存在

r - 根据其他变量中的顺序为新变量赋值

r - 变量的无效类型(列表)

mysql - 在 Mac OS X 上使用 R 访问 MySQL 数据库

LaTeX Beamer -- 如何在没有表格环境的情况下创建编号表格标题

r - 有没有办法使用 bookdown 添加章节引用书目?

r - 编译 pdf 输出时knitr -pandoc-citeproc 错误