Search code examples
rr-markdownknitrkablepander

table with long text, bullet points and specific table width


I want a table to have bullet points in one column and to have a specific table width (in order to be placed on one page when rendered to PDF).

How can I achieve this in rmarkdown using one of the many packages out there?


What I have tried and have so far:

---
output: pdf_document
---

```{r, include = FALSE}
df <- data.frame(col1 = "Some really long text here. I mean some reeeeeaaly loooong text. So long, it should be wrapped. Really.",
                 col2 = "* bullet point 1\n * bullet point 2", col3 = "Yes, there is still another column.")
```

# Attempt 1: kableExtra
```{r, echo = FALSE, warning = FALSE}
library(kableExtra)
df1 <- df
df1$col2 <- linebreak(df1$col2)
knitr::kable(df1, escape = FALSE) %>% column_spec(1, width = "15em")
```

# Attempt 2: pander
```{r, echo = FALSE}
pander::pander(df, keep.line.breaks = TRUE, style = 'grid', justify = 'left')
```

This renders to:

enter image description here

As you can see both options have caveats. The kableExtra version does have a specific table width that fits on one page but does not show bullet points nicely. Whereas the pander solution renders the bullet points nicely but spans multiple pages, because I don't know how to specifiy the table width in pander.

Is there a solution that can do both?

Related questions are for example here and there.


Solution

  • Use the split.table parameter of pandoc.table (that is being called by pander in the background) or disable the table splitting in general via panderOptions's table.split.table, eg

    pander::pander(df, keep.line.breaks = TRUE, style = 'grid', justify = 'left', split.table = Inf)
    

    or

    library(pander)
    panderOptions('table.style', 'grid')
    panderOptions('table.alignment.default', 'left')
    panderOptions('table.split.table', Inf)
    panderOptions('keep.line.breaks', TRUE)
    pander(df)
    

    pander example