ddesmet
ddesmet

Reputation: 31

Can I add lines that will visually delineate groups of columns with KableExtra?

I'm trying make a table with the following code:

tabledf[1:12,] %>%
  kbl(escape = F, booktabs = T) %>%
  column_spec(1, width="2in", border_left = T, border_right=T) %>%
  column_spec(2:ncol(df), width="1in", border_left = T, border_right=T) %>%
  column_spec(6, border_right="4px solid black") %>%
  column_spec(1, background = "#E8DFE4") %>%
  kable_paper() %>%
  row_spec(row=0, align="c") %>%
  pack_rows("Conflict over decision to pursue and implement GE", 1, 5, label_row_css = "background-color: #666; color: #fff;") %>%
  pack_rows("General agreement to implement GE, but conflicts emerge over: ", 6, 10, label_row_css = "background-color: #666; color: #fff;") %>%
  pack_rows("Second-order geopolitical risks", 11, 12, label_row_css = "background-color: #666; color: #fff;") %>%
  add_header_above(c("", "Existing Legal Instruments" = 5, "Proposed Legal Instruments"=4)) %>%
  # pack_rows("Solar Radiation Management", 14, 16, label_row_css = "background-color: #666; color: #fff;") %>%
  # pack_rows("Carbon Dioxide Removal", 17, 20, label_row_css = "background-color: #666; color: #fff;") %>%
  save_kable("table3.png", zoom=1.5)

I'd like to try and make a table that has visual demarcations in the column name row to better visually see groupings. I've tried border_left and border_right as TRUE, but it doesn't seem to be working. I've tried setting line to TRUE, but I realized that this was already the default. At one point, I had a setting on that was actually generating a nice looking grey line underneath the grouping titles, but I accidentally didn't save that and I have no idea what the setting was.

Does anyone have experience with this issue? I've read the documentation probably half a dozen times and don't know what I'm missing.

edit:

Here should be reproducible code that should help illustrate what I'm not seeing how to do:

library(kableExtra)
library(tidyverse)

tempdf <- mtcars[1:6,1:5]
tempdf %>% 
  kbl(escape = F, booktabs = T) %>%
  column_spec(3, border_right="4px solid black") %>%
  kable_paper() %>%
  add_header_above(c("", "Group One" = 2, "Group Two"=3))

Here's what this could should produce. I'd like to get a visual element that would separate the groups through the column name row.

Upvotes: 3

Views: 1750

Answers (1)

bttomio
bttomio

Reputation: 2306

Maybe this could help you. I just added border_right = T to separate each group (Group One = columns 1 and 3; Group Two = columns 3 and 6).

Simple .Rmd file to generate the table:

---
title: "Untitled"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(kableExtra)
library(tidyverse)
```

## Borders by group

```{r}
tempdf <- mtcars[1:6,1:5]
tempdf %>% 
  kbl(escape = F, booktabs = T, linesep = "") %>%
  add_header_above(c("", "Group One" = 2, "Group Two"=3)) %>%
  column_spec(1, border_right=T) %>%
  column_spec(3, border_right=T) %>%
  column_spec(6, border_right=T) %>%
  kable_paper() %>%
  kable_styling(latex_options =c("hold_position"))

```

-output

enter image description here

Upvotes: 1

Related Questions