Search code examples
rplyrsweavextableknitr

xtable for arrays


I'm trying to use xtable for 3-dimensional array. My minimal example is

Test <- 
structure(1:8, .Dim = c(2L, 2L, 2L), .Dimnames = list(c("A1", 
"A2"), c("B1", "B2"), c("C1", "C2")))

library(plyr)
library(xtable)

a_ply(.data=Test, .margins=3, function(i) {
  xtable(x = Test[, , i])
      }
)

This produces the following error:

  Error in xtable(x = Test[, , i]) : subscript out of bounds

I'd appreciate if you give me some pointers to resolve this problem. Thanks in advance.


Solution

  • a_ply doesn't return anything so hopefully your function saves these or something along those lines. the i you're passing to the function is the subset of your array based on the margins you provide. so you're sending it the 2x2 array C1 then the 2x2 array C2:

    a_ply(Test, 3, function(i) {print(i); print('-----')})
    

    so indexing into your Test array with i doesn't make sense.

    why not just:

    apply(Test, 3, xtable)
    

    or using plyr:

    alply(Test, 3, xtable)
    

    For knitr:

    a_ply(Test, 3, function(i) print(xtable(i)))