Rapporter / pander

An R Pandoc Writer: Convert arbitrary R objects into markdown
http://rapporter.github.io/pander/
Open Software License 3.0
294 stars 66 forks source link

Print table with pander #309

Closed GaryDe2 closed 7 years ago

GaryDe2 commented 7 years ago

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)

If I convert it in data frame beforecalling pander it works, but the format is now other, it isn't Var1 (mpg in exemple) as row names and Var2 (cyl) as column names, but Var1 (mpg) as column 1, Var2 (cyl) as column 2, and frequencies as column 3, what isn't the researched output when printing a table.

> devtools::session_info()
Session info -------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.1 (2017-06-30)
 system   i386, mingw32               
 ui       RStudio (1.0.153)           
 language (EN)                        
 collate  German_Switzerland.1252     
 tz       Europe/Berlin               
 date     2017-08-30                  

Packages -----------------------------------------------------------------------
 package      * version  date       source        
 acepack        1.4.1    2016-10-29 CRAN (R 3.4.1)
 assertthat     0.2.0    2017-04-11 CRAN (R 3.4.1)
 backports      1.1.0    2017-05-22 CRAN (R 3.4.0)
 base         * 3.4.1    2017-06-30 local         
 base64enc      0.1-3    2015-07-28 CRAN (R 3.4.0)
 bindr          0.1      2016-11-13 CRAN (R 3.4.1)
 bindrcpp       0.2      2017-06-17 CRAN (R 3.4.1)
 cellranger     1.1.0    2016-07-27 CRAN (R 3.4.1)
 checkmate      1.8.3    2017-07-03 CRAN (R 3.4.1)
 cluster        2.0.6    2017-03-10 CRAN (R 3.4.1)
 colorspace     1.3-2    2016-12-14 CRAN (R 3.4.1)
 compiler       3.4.1    2017-06-30 local         
 cowplot      * 0.8.0    2017-07-30 CRAN (R 3.4.1)
 curl           2.8.1    2017-07-21 CRAN (R 3.4.1)
 data.table     1.10.4   2017-02-01 CRAN (R 3.4.0)
 datasets     * 3.4.1    2017-06-30 local         
 devtools       1.13.3   2017-08-02 CRAN (R 3.4.1)
 digest         0.6.12   2017-01-27 CRAN (R 3.4.1)
 dplyr        * 0.7.2    2017-07-20 CRAN (R 3.4.1)
 forecast     * 8.1      2017-06-17 CRAN (R 3.4.1)
 foreign        0.8-69   2017-06-22 CRAN (R 3.4.1)
 Formula      * 1.2-2    2017-07-10 CRAN (R 3.4.1)
 fracdiff       1.4-2    2012-12-02 CRAN (R 3.4.1)
 ggplot2      * 2.2.1    2016-12-30 CRAN (R 3.4.1)
 glue           1.1.1    2017-06-21 CRAN (R 3.4.1)
 graphics     * 3.4.1    2017-06-30 local         
 grDevices    * 3.4.1    2017-06-30 local         
 grid           3.4.1    2017-06-30 local         
 gridExtra      2.2.1    2016-02-29 CRAN (R 3.4.1)
 gtable         0.2.0    2016-02-26 CRAN (R 3.4.1)
 HistData     * 0.8-2    2017-08-16 CRAN (R 3.4.1)
 Hmisc        * 4.0-3    2017-05-02 CRAN (R 3.4.1)
 htmlTable      1.9      2017-01-26 CRAN (R 3.4.1)
 htmltools      0.3.6    2017-04-28 CRAN (R 3.4.1)
 htmlwidgets    0.9      2017-07-10 CRAN (R 3.4.1)
 knitr        * 1.17     2017-08-10 CRAN (R 3.4.1)
 lattice      * 0.20-35  2017-03-25 CRAN (R 3.4.1)
 latticeExtra   0.6-28   2016-02-09 CRAN (R 3.4.1)
 lazyeval       0.2.0    2016-06-12 CRAN (R 3.4.1)
 lmtest         0.9-35   2017-02-11 CRAN (R 3.4.1)
 lubridate    * 1.6.0    2016-09-13 CRAN (R 3.4.1)
 magrittr       1.5      2014-11-22 CRAN (R 3.4.1)
 MASS         * 7.3-47   2017-02-26 CRAN (R 3.4.1)
 Matrix         1.2-11   2017-08-16 CRAN (R 3.4.1)
 memoise        1.1.0    2017-04-21 CRAN (R 3.4.1)
 methods      * 3.4.1    2017-06-30 local         
 munsell        0.4.3    2016-02-13 CRAN (R 3.4.1)
 nnet           7.3-12   2016-02-02 CRAN (R 3.4.1)
 pander       * 0.6.1    2017-08-06 CRAN (R 3.4.1)
 parallel       3.4.1    2017-06-30 local         
 pkgconfig      2.0.1    2017-03-21 CRAN (R 3.4.1)
 plyr           1.8.4    2016-06-08 CRAN (R 3.4.1)
 quadprog       1.5-5    2013-04-17 CRAN (R 3.4.0)
 quantmod       0.4-10   2017-06-20 CRAN (R 3.4.1)
 R6             2.2.2    2017-06-17 CRAN (R 3.4.1)
 raster       * 2.5-8    2016-06-02 CRAN (R 3.4.1)
 RColorBrewer * 1.1-2    2014-12-07 CRAN (R 3.4.0)
 Rcpp           0.12.12  2017-07-15 CRAN (R 3.4.1)
 readxl       * 1.0.0    2017-04-18 CRAN (R 3.4.1)
 rgdal        * 1.2-8    2017-07-01 CRAN (R 3.4.1)
 rgeos        * 0.3-23   2017-04-06 CRAN (R 3.4.1)
 rlang          0.1.2    2017-08-09 CRAN (R 3.4.1)
 rpart          4.1-11   2017-03-13 CRAN (R 3.4.1)
 scales         0.5.0    2017-08-24 CRAN (R 3.4.1)
 sp           * 1.2-5    2017-06-29 CRAN (R 3.4.1)
 splines        3.4.1    2017-06-30 local         
 stats        * 3.4.1    2017-06-30 local         
 stringi        1.1.5    2017-04-07 CRAN (R 3.4.0)
 stringr        1.2.0    2017-02-18 CRAN (R 3.4.1)
 survival     * 2.41-3   2017-04-04 CRAN (R 3.4.1)
 tibble         1.3.4    2017-08-22 CRAN (R 3.4.1)
 timeDate       3012.100 2015-01-23 CRAN (R 3.4.0)
 tools          3.4.1    2017-06-30 local         
 tseries        0.10-42  2017-06-22 CRAN (R 3.4.1)
 TTR            0.23-2   2017-07-11 CRAN (R 3.4.1)
 UsingR       * 2.0-5    2015-08-06 CRAN (R 3.4.1)
 utils        * 3.4.1    2017-06-30 local         
 viridis      * 0.4.0    2017-03-27 CRAN (R 3.4.1)
 viridisLite  * 0.2.0    2017-03-24 CRAN (R 3.4.1)
 withr          2.0.0    2017-07-28 CRAN (R 3.4.1)
 xtable       * 1.8-2    2016-02-05 CRAN (R 3.4.1)
 xts            0.10-0   2017-07-07 CRAN (R 3.4.1)
 zoo            1.8-0    2017-04-12 CRAN (R 3.4.1)

Windows 7 Proffessional, Service Pack 1, 32-bits

daroczig commented 7 years ago

Interesting -- this works fine here with the same (CRAN) version:

> pander(table(mtcars$am, mtcars$cyl))

---------------------
 &nbsp;   4   6   8  
-------- --- --- ----
 **0**    3   4   12 

 **1**    8   3   2  
---------------------

I don't know how this could happen on your machine -- I keep thinking if any Windows-specific issues might cause this, but I don't have any ideas right now. I'm on Linux BTW:

> devtools::session_info()
Session info ------------------------------------------------------------------
 setting  value                       
 version  R version 3.4.1 (2017-06-30)
 system   x86_64, linux-gnu           
 ui       X11                         
 language (EN)                        
 collate  en_US.UTF-8                 
 tz       Europe/Budapest             
 date     2017-08-30                  

Packages ----------------------------------------------------------------------
 package   * version date       source        
 base      * 3.4.1   2017-07-08 local         
 compiler    3.4.1   2017-07-08 local         
 datasets  * 3.4.1   2017-07-08 local         
 devtools    1.13.2  2017-06-02 CRAN (R 3.4.0)
 digest      0.6.12  2017-01-27 CRAN (R 3.4.0)
 graphics  * 3.4.1   2017-07-08 local         
 grDevices * 3.4.1   2017-07-08 local         
 memoise     1.1.0   2017-04-21 CRAN (R 3.4.0)
 methods   * 3.4.1   2017-07-08 local         
 pander    * 0.6.1   2017-08-06 CRAN (R 3.4.1)
 Rcpp        0.12.11 2017-05-22 CRAN (R 3.4.0)
 stats     * 3.4.1   2017-07-08 local         
 tcltk       3.4.1   2017-07-08 local         
 tools       3.4.1   2017-07-08 local         
 utils     * 3.4.1   2017-07-08 local         
 withr       1.0.2   2016-06-20 CRAN (R 3.3.1)
GaryDe2 commented 7 years ago

I think I just figured why: I had forgotten I used this at some place:

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

It seems that without this option it works. But I have no idea why it doesn't work with, and then doesn't know how to fix the alignment AND printing tables together...

daroczig commented 7 years ago

Ah, got it -- then it's not a bug in pander per se. You have to modify the anonymous function used to decide on the alignment, eg:

> panderOptions('table.alignment.default', function(df) ifelse(sapply(as.data.frame(df), is.numeric), 'right', 'left'))
> pander(table(mtcars$am, mtcars$cyl))

---------------------
 &nbsp;  4   6      8
-------- --- --- ----
 **0**   3   4     12

 **1**   8   3      2
---------------------
GaryDe2 commented 7 years ago

Thanks! Sorry for not having seen that before, it just totally escaped from my mind