2
votes

I am trying to write a brew template that takes a dataset, and produces a simple data dictionary.

I want a separate page with the name of the var, and a frequency table for that var so I have this written so far:

An R function that returns a frequency table:

#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)
}

I then apply the function in a Brew loop:

  <% 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
<% } -%>

I get an error "in cat(list(...),file,sep,fill,labels,append): argument 1 (type 'list') cannot be handled by 'cat'

I know this is coming from the <%= xtable(procfreq(testData[,i]),names(testData)[i],names(testData)[i]) %> statement, and the error goes away if I wrap it in print(xtable(procfreq(testData[,i]),names(testData)[i],names(testData)[i])) However, I now get the xtable LaTeX output for each table TWICE for some reason, which is a major problem since manually deleting the extra tables defeats the purpose of automating the reports.

After looking at https://learnr.wordpress.com/2009/09/09/brew-creating-repetitive-reports/#X12 I tried using the include_tbl function, which appears to use the same print(xtable(...)) concept I was already using, and I get the same problem with the xtable LaTeX output appearing twice in each loop.

Since the article was written 6 years ago, I'm guessing something in R has changed since then that has affected the functionality of the example.

As far as I can tell, there is no way for Brew templates to use R tables without duplicating them, which can't possibly be true.

Lastly, my session info:

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 
2

2 Answers

2
votes

I believe the problem is that xtable's output is meant to be printed, not fed to a cat. This seems to work,

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
<% } -%>

I wonder how many spaces could be removed before magrittr/brew get confused.

1
votes

As the question have the pander tag, please let me post an answer using its improved brew function. Content of the report template file:

<% for (varname in
        tail(names(mtcars), 4)) { # start looping %>

## <%= varname %>

<%= ## results will be automatically passed to `pander`
rapportools::rp.freq(varname, mtcars)
%>

<% }                              # end loop %>

And brewing it via pander::Pandoc:brew:

> Pandoc.brew('demo.brew')

## vs

--------------------------------------
 vs    N    %     Cumul. N   Cumul. % 
----- --- ------ ---------- ----------
  0   18  56.25      18       56.25   

  1   14  43.75      32       100.00  

Total 32  100.00     32       100.00  
--------------------------------------

## am

---------------------------------------
 am    N     %     Cumul. N   Cumul. % 
----- --- ------- ---------- ----------
  0   19  59.375      19       59.375  

  1   13  40.625      32      100.000  

Total 32  100.000     32      100.000  
---------------------------------------

## gear

----------------------------------------
 gear   N     %     Cumul. N   Cumul. % 
------ --- ------- ---------- ----------
  3    15  46.875      15       46.875  

  4    12  37.500      27       84.375  

  5     5  15.625      32      100.000  

Total  32  100.000     32      100.000  
----------------------------------------

## carb

----------------------------------------
 carb   N     %     Cumul. N   Cumul. % 
------ --- ------- ---------- ----------
  1     7  21.875      7        21.875  

  2    10  31.250      17       53.125  

  3     3   9.375      20       62.500  

  4    10  31.250      30       93.750  

  6     1   3.125      31       96.875  

  8     1   3.125      32      100.000  

Total  32  100.000     32      100.000  
----------------------------------------