-->

Change default alignment in pander (pandoc.table)

2019-04-29 05:48发布

问题:

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
 ))

回答1:

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.