Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change default alignment in pander (pandoc.table)

I am currently switching to pander for most of my knitr-markdown formatting, because it provides such great pandoc support. One of the things I am not so happy with is the default center-alignment. Marketing people may love it, but for technical reports it is an horror.

The best choice used by Hmisc is to use left alignment for texts and dates by default, and right alignment for number of all type.

Is there a simple way to get this globally set in pander?

library(pander)
pander(data.frame(
     name          = letters[1:3],
     size          = 1:3,
     we.have.dates = Sys.Date() - 1:3
 ))
like image 366
Dieter Menne Avatar asked Nov 19 '14 10:11

Dieter Menne


1 Answers

Thanks for you kind words and great question. There's a not yet well documented feature in pander, but you can also pass an R function as the default table alignment. Quick demo:

> panderOptions('table.alignment.default',
+     function(df) ifelse(sapply(df, is.numeric), 'right', 'left'))
> pander(data.frame(
+     name          = letters[1:3],
+     size          = 1:3,
+     we.have.dates = Sys.Date() - 1:3
+ ))

-----------------------------
name     size we.have.dates  
------ ------ ---------------
a           1 2014-11-18     

b           2 2014-11-17     

c           3 2014-11-16     
-----------------------------

So the trick here is to define a function which takes only one argument to be analysed, and it returns the vector of column alignment parameters.

like image 190
daroczig Avatar answered Sep 27 '22 18:09

daroczig