0
votes

I try to write an R-function which produces xtables in a loop. Later I want to call my function in a Sweave document- but a single chunk can't support multiple tables. I would have to put each table in a single chunk and wrap it with the Latex Code \begin{table} ... \end{table}. So I wonder, whether it's possible to somehow call Sweave/knitr from within the Loop of the R-function and add \begin{table} .. \end{table} around each xtable? Or whether it is somehow possible to send each xtable from the loop to a chunk with \begin{table} ... \end{table} environment?

A mini-example of my function:

multiple_tables_Loop<-function(...){

(....) ##Some necessary calculations to produce a data frame

  for(j in 1:m){
    for(i in 1:n){

      a<-data.frame(...)   

      table<-xtable(a)

      print(table)
     } 
   }
 }

In Sweave I would call the function:

<<Hallo_Table,results='aisis'>>
 multiple_tables_Loop(...)
@
1

1 Answers

1
votes

I'm confused by your question. xtable does include \begin{table}/\end{table} pairs. And you can put multiple tables is a code chunk (for both Sweave and knitr .Rnw files). Could it be just that you have misspelled 'asis' in your chunk header?

Showing xtable does include \begin{table}/\end{table}:

> xtable(data.frame(x=1))
% latex table generated in R 3.1.2 by xtable 1.7-4 package
% Fri Jan 23 11:12:47 2015
\begin{table}[ht]
\centering
\begin{tabular}{rr}
  \hline
 & x \\ 
  \hline
1 & 1.00 \\ 
   \hline
\end{tabular}
\end{table}

And a simple .Rnw file of

<<results="asis">>=
library("xtable")
xtable(data.frame(x=1))
xtable(data.frame(y=1))
@

properly gives two tables.

If the misspelling isn't the problem, a complete minimally reproducible example is needed along with the version numbers of R and all the packages (output of sessionInfo())