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
 ))
Henrik
  • 65,555
  • 14
  • 143
  • 159
Dieter Menne
  • 10,076
  • 44
  • 67

1 Answers1

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.

daroczig
  • 28,004
  • 7
  • 90
  • 124
  • Why isn't this default? Was pander created by "marketing people"? – Waldir Leoncio May 29 '15 at 20:00
  • 1
    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! – Waldir Leoncio May 29 '15 at 20:04
  • 1
    @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. – daroczig May 30 '15 at 02:00
  • 3
    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. – Waldir Leoncio Jun 01 '15 at 12:23