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:
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.
Create some bullets in a table, then from the Styles task pane, right-click List Paragraph and click Modify. Click Format at bottom left, then click Paragraph. Select your required Indentation from the Left side of the cell and your required Hanging indent size then click OK.
Click on the word document so that your cursor is inside of the first column and then click "Home" at the top of the screen. Choose "Paragraph" from the resulting options and then choose "Bullets and Numbering" to add your bullet points.
You can create a numbered list with LaTex bullet points with the same code we used before, except with \begin{enumerate} and \end{enumerate} around the list items instead of \begin{itemize} and \end{itemize}.
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:
\cdots
from LaTex are employed as bullets (alternatives see here)\n
(indentation is therefore not as nice as with the pander
approach from @daroczig).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://stackoverflow.com/questions/53794142/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 -->
I also tried the pander
approach from @daroczig and made the following experiences:
$\boldsymbol{\cdot}$
-workaround in the kableExtra
approach).In addition, when using the pander
approach in an Rmd file using the huskydown thesis template the footnotes greatly messed up the table alignment.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With