rpander

R: print table with pander


When printing a table with pander I obtain an error message Error in pandoc.table.return(...) : Wrong number of parameters (76 instead of *4*) passed: justify that I can't understand.

a <- table(mtcars$mpg, mtcars$cyl)
pander(a)

Traceback:

6. stop(sprintf("Wrong number of parameters (%s instead of *%s*) passed: justify", length(justify), length(t.width)))
5. pandoc.table.return(...)
4. cat(pandoc.table.return(...))
3. pandoc.table(x, caption = caption, ...)
2. pander.table(a)
1. pander(a)

What am I doing false? My goal is to print the table in the table format (values of variable 1 as row names, values of variable 2 as column names), and not as it is if I convert the table into a dataframe (values of variable 1 in column 1, values of variable 2 in column 2, frequency in column 3). I know it would work with print, but I would like to have the pander layout because all my other tables (from data frame format) are printed with pander.


Solution

  • I realised I had forgotten but I had that at some place:

    panderOptions('table.alignment.default',
         function(df) ifelse(sapply(df, is.numeric), 'right', 'left'))
    

    Replacing it with:

    panderOptions('table.alignment.default',
         function(df) ifelse(sapply(as.data.frame(df), is.numeric), 'right', 'left'))
    

    works fine.

    Thanks @daroczig for finding that.