table with long text, bullet points and specific table width
Asked Answered
O

2

7

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.

Orgy answered 24/10, 2018 at 6:38 Comment(0)
E
1

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

Erb answered 24/10, 2018 at 6:55 Comment(2)
Thanks, @daroczig. Your solution works fine for the example in the question. In my real example, however, the table is still split on two pages. Do you have any idea why this still can happen? Are the column texts far too long?Orgy
@Erb Sometimes I have to work with grid tables/pander tables. If they contain a bullet list, they always produce a blank space right after the list, just as when I compile your code. Would you have any idea where this blank space could stem from and how to avoid/get rid of it?Cowman
C
3

An alternative solution based on kableExtra (optional: with footnotes)

This approach allows to add a caption, to manually add footnotes within the table, and to fix column widths.

To create the bullet point list:

  • "thicker" \cdots from LaTex are employed as bullets (alternatives see here)
  • linebreaks are forced with \n (indentation is therefore not as nice as with the pander approach from @daroczig).

MWE

library(stringi); library(kableExtra); library(dplyr)
string_short <- "Descriptor"
string_long <- substr(stri_rand_lipsum(1), 1, 50)

# add footnotes manually within table
string_bulletlist <- "$\\boldsymbol{\\cdot}$ bullet point 1: foo$^{a}$ \n $\\boldsymbol{\\cdot}$ bullet point 2: bar$^{b}$"

df <- data.frame(col1 = c(string_short, string_short),
                 col2 = c(string_bulletlist, string_bulletlist),
                 col3 = c(string_long, string_long)
)
col_names <- c("Descriptor", "Description with list", "Some comment")

# header: bold column names
colnames(df) <- paste0("\\textbf{", col_names,"}")

# add footnote with kableExtra commands
names(df)[1] <- paste0(names(df)[1], footnote_marker_symbol(1))

df %>%
  mutate_all(linebreak) %>% # required for linebreaks to work
  kable(
    "latex",  
    escape = F, 
    booktabs=TRUE, 
    align = "l",
    caption = 'kableTable with bullet list and footnote') %>%
  # kable_styling(full_width = F) %>% # makes no difference here
  footnote(general = "General comment of the table.",
           alphabet = c("Footnote A;", "Footnote B;"),
           symbol = c("Footnote Symbol 1")) %>% 
  column_spec(1, width = "5em") %>% # fix width column 1
  column_spec(2, width = "10em") %>% # fix width column 2
  column_spec(3, width = "15em") # fix width column 3

To [improve line spacing[(https://mcmap.net/q/959005/-increase-line-row-spacing-with-kableextra), one can add the following before & after code chunk in the Rmd:

\renewcommand{\arraystretch}{1.5} <!-- increase line spacing for the table -->
RMD CHUNK HERE
\renewcommand{\arraystretch}{1} <!-- reset row hight/line spacing -->

kableExtra table

Comment:

I also tried the pander approach from @daroczig and made the following experiences:

  • Pro: nice line spacing plus a "real bullet list" (compared to the $\boldsymbol{\cdot}$-workaround in the kableExtra approach).
  • Con: a large blank space is included after the bullet list

In addition, when using the pander approach in an Rmd file using the huskydown thesis template the footnotes greatly messed up the table alignment.

Cowman answered 3/7, 2020 at 11:58 Comment(0)
E
1

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

Erb answered 24/10, 2018 at 6:55 Comment(2)
Thanks, @daroczig. Your solution works fine for the example in the question. In my real example, however, the table is still split on two pages. Do you have any idea why this still can happen? Are the column texts far too long?Orgy
@Erb Sometimes I have to work with grid tables/pander tables. If they contain a bullet list, they always produce a blank space right after the list, just as when I compile your code. Would you have any idea where this blank space could stem from and how to avoid/get rid of it?Cowman

© 2022 - 2024 — McMap. All rights reserved.