pandoc.list

One of my recent tortures has been formatting RMarkdown reports. The pander package gives you access to all the Pandoc options you can handle. Seems like a big mountain to climb, but once you get started it’s actually not too rough.

In particular, the pandoc.[WHATEVER]() functions from pander make it easy to render nicely formatted objects in RMarkdown.

An example … say you had the following (not so arbitrary) character vector:

bowie <- c("Young Americans", "Station To Station", "Low", "Heroes", "Lodger", "Scary Monsters", "Let's Dance")

You could just print the object:

bowie
## [1] "Young Americans"    "Station To Station" "Low"               
## [4] "Heroes"             "Lodger"             "Scary Monsters"    
## [7] "Let's Dance"

Doesn’t look great. Maybe try it with the generic pander() function around the name of the character vector:

# install.packages("pander")
library(pander)
pander(bowie)

Young Americans, Station To Station, Low, Heroes, Lodger, Scary Monsters and Let’s Dance

Still not great, but until a couple days ago that’s probably where I would have left it.

Then I tab-completed into the pandoc.[WHATEVER]() functions and realized I was doing it wrong …

In this case if I want a list then I should go with pandoc.list():

pandoc.list(bowie)
## 
## * Young Americans 
## * Station To Station 
## * Low 
## * Heroes 
## * Lodger 
## * Scary Monsters 
## * Let's Dance 
## 
## <!-- end of list -->

That looks more like a list … at least as it would be printed in a console.

If you want to see it bulleted just wrap another pander() around it:

pander(pandoc.list(bowie))
  • Young Americans
  • Station To Station
  • Low
  • Heroes
  • Lodger
  • Scary Monsters
  • Let’s Dance

And if you want to see the vector ordered:

pander(pandoc.list(bowie, style="ordered"))
  1. Young Americans
  2. Station To Station
  3. Low
  4. Heroes
  5. Lodger
  6. Scary Monsters
  7. Let’s Dance

The best part about these functions is that they’re documented inside R man pages (as opposed to only in the generic pandoc documentation) … check it out:

?pandoc.list
sessionInfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Mojave 10.14.6
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] pander_0.6.3
## 
## loaded via a namespace (and not attached):
##  [1] compiler_3.6.1  magrittr_1.5    bookdown_0.12   tools_3.6.1    
##  [5] htmltools_0.3.6 yaml_2.2.0      Rcpp_1.0.2      stringi_1.4.3  
##  [9] rmarkdown_1.14  blogdown_0.14   knitr_1.23      stringr_1.4.0  
## [13] digest_0.6.21   xfun_0.8        evaluate_0.14