How to write (bullet) lists in a table using rmarkdown and pandoc
Asked Answered
R

1

4

I am looking to create a table in a PDF document using rmarkdown, knitr and pander. The table should be nearly identical to Table 1 shown below, except the asterisks should be bullets. Is this at all possible using only the R libraries listed above?

sampletable

Here is my code to produce the PDF document (and thus the table above):

---
title: "xxx"
author: "xxx"
date: "xxx"
output:
  word_document: default
  pdf_document:
    fig_height: 4
    fig_width: 10
    highlight: tango
geometry: margin=3cm
---

```{r global_options, include=FALSE, echo=FALSE}
require(knitr)
opts_chunk$set(fig.width=8, fig.height=4, fig.path='figs/', dpi=500,
               echo=FALSE, warning=FALSE, message=FALSE, results='hide')
```

```{r pandoc_options, include=FALSE, echo=FALSE}
require(pander)
panderOptions('digits', 3)
panderOptions('round', 3)
panderOptions('keep.trailing.zeros', TRUE)
panderOptions('keep.line.breaks', TRUE)
```

```{r concepts, echo=FALSE}
mytable = data.frame(Concept = c("Decoded", "XXX"), 
                     Description = c(" 
                                     \\\n
                                     \\\n * Founded in 2011 
                                     \\\n * * Offers workshops to take people from zero skills and knowledge in programming through to coding a multi-platform app using HTML, CSS and Javascript in a single day 
                                     \\\n * * Rave reviews", "XXX"), 
                     Website = c("http://decoded.com/uk/","XXX"))

```

``` {r concepts_descriptions, results = 'asis'}
pandoc.table(mytable, style = "multiline", justify = "left", caption = "Concepts and Descriptions")
```

EDIT @Roman thanks for that - however if I simply substitute, I get the following not-so-beautiful table ("period" bullets, poor formatting) ... The most important thing for me now is the formatting that comes with lists. Thanks!

samplepic_bullet

Rockribbed answered 23/6, 2015 at 19:0 Comment(2)
In theory, you could replace the asterix with a character of a bullet (•). You can do that with a simple gsub call -- gsub("\\*", "•", "* a list item").Bombe
My understanding is that pandoc uses LaTeX as an intermediat format. Have you tried replacing the * with the LaTeX bullet character ($\bullet$)? (Sorry I don't have time to check if this works just now.) The use of cdot in this SO question may be useful.Condottiere
G
9

The default multiline style table does not support arbitrary block elements inside of the cells, but the grid tables does. So this is possible, just make sure:

  • you use grid style
  • align the cell to the left
  • use hard line breaks at the end of the list elements and enable keep.line.break

Quick demo:

mytable = data.frame(
    Concept     = c("Decoded", "XXX"),
    Description = c("* Founded in 2011\ \n* Offers workshops to take people from zero skills and knowledge in programming through to coding a multi-platform app using HTML, CSS and Javascript in a single day", "XXX"),
    Website     = c("http://decoded.com/uk/","XXX"))

pander::pander(mytable, keep.line.breaks = TRUE, style = 'grid', justify = 'left')

Resulting in a nicely formatted HTML list via pandoc:

<table>
<colgroup>
<col width="13%" />
<col width="43%" />
<col width="30%" />
</colgroup>
<thead>
<tr class="header">
<th align="left">Concept</th>
<th align="left">Description</th>
<th align="left">Website</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Decoded</td>
<td align="left">* Founded in 2011 * Offers workshops to take people from zero skills and knowledge in programming through to coding a multi-platform app using HTML, CSS and Javascript in a single day</td>
<td align="left">http://decoded.com/uk/</td>
</tr>
<tr class="even">
<td align="left">XXX</td>
<td align="left">XXX</td>
<td align="left">XXX</td>
</tr>
</tbody>
</table>

But works with PDF as well:

enter image description here

Grith answered 23/6, 2015 at 21:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.