Search code examples
rstringdataframepander

Conveniently inserting custom text in table headers generated by pander


I'm generating a simple table in pander. I would like to have a greater control over the text that appears as a header. For example the code below:

---
title: "OddFile"
author: "Test"
date: "November 18, 2015"
output: pdf_document
---

Amazing table

```{r sourceDescribeIndicators, echo=FALSE, eval=TRUE, message=FALSE, warning=FALSE, cache=TRUE}
library(datasets)
data(mtcars)
require(reshape2)
mtcars$nme <- rownames(mtcars)
mtc_melt <- melt(data = mtcars, id.vars = c("nme"))

# Pander
require(pander)
pander(data.frame("Available Models" = unique(mtc_melt$nme)))
```

Would generate the following table:

pander table

The data frame transformation applied to the generated vector of unique values results in Available Models being transformed to Available.Models as the make.names function would return the same result. How can I conveniently work around it? I'm interested in a solution that would enable me to use strings that are not syntactically correct when generating tables via pander.


Solution

  • This is not a pander issue, but data.frame calls make.names internally by default, but you can override this behavior by the check.names argument, eg:

    > pander(head(data.frame("Available Models" = unique(mtc_melt$nme), check.names = FALSE)))
    
    ------------------
     Available Models 
    ------------------
        Mazda RX4     
    
      Mazda RX4 Wag   
    
        Datsun 710    
    
      Hornet 4 Drive  
    
    Hornet Sportabout 
    
         Valiant      
    ------------------