Change default alignment in pander (pandoc.table)
Asked Answered
C

1

7

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
 ))
Complacence answered 19/11, 2014 at 10:11 Comment(0)
A
11

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.

Assembler answered 19/11, 2014 at 10:28 Comment(4)
Why isn't this default? Was pander created by "marketing people"?Decay
By the way, I've used this on some 2-way tables I was creating. Had to substitute your sapply function to apply(df, 2, is.numeric) for it to work. Thanks for the answer!Decay
@WaldirLeoncio wow, I was never referred to as "marketing guy" :) About default values: these will never fit everyone's liking, but that's why I created global panderOptions -- which you can put to your .Rprofile as well.Assembler
Thanks for the reply and for making such customization a possibility. Also, thank you for taking my joke lightly, I didn't mean to be offensive. I do have the feeling such configuration would appease the majority of people using R and pander, though, so I kindly ask you to consider making it default.Decay

© 2022 - 2024 — McMap. All rights reserved.