Is there a way to make a kable without lines/borders for pdf?
Asked Answered
B

3

7

I'm working on a shiny-app that produces and sends a pdf-report, containing the wrangled data. The problem is that I can't get the table layout to look as the client want it to look.

The client wants the tables to lack lines/borders except ontop of the last row, is this possible in kable and/or kableExtra? No answers containing other packages please, as I'm aware that of xtable.

table.tbl <- tibble(var1 = c("entry 1", "entry 2", "entry 3", "entry 4"),
                var2 = c(2000, 1000, 3000, 200),
                var3 = c(3000, 2000, 4000, 100))

table.tbl %>% 
  kable("latex", 
        booktabs = T) %>% 
  row_spec((table.tbl %>% 
             nrow()-1), hline_after = T)
Blanca answered 20/12, 2018 at 12:7 Comment(0)
L
9

I think kable is meant to be super simple and so lacks features like this by design. That said, I have come up with an absurdly painful solution. The gist is that I set border colours to white (I'm assuming your page is white), then switch line colours to non-white (red in my example) when needed, then back to white again afterwards.

Initially, add the following to your YAML header:

header-includes:
  - \usepackage{colortbl}

Next, in your document add:

\arrayrulecolor{white}

To render the table use:

library(tidyverse)
library(knitr)
library(kableExtra)

table.tbl <- tibble(var1 = c("entry 1", "entry 2", "entry 3", "entry 4"),
                var2 = c(2000, 1000, 3000, 200),
                var3 = c(3000, 2000, 4000, 100))
table.tbl %>% 
  kable(format = "latex") %>%
  row_spec((table.tbl %>% 
             nrow()-1), extra_latex_after = "\\arrayrulecolor{red}") %>% 
  row_spec((table.tbl %>% 
             nrow()), extra_latex_after = "\\arrayrulecolor{white}")

giving,

enter image description here

Lenorelenox answered 20/12, 2018 at 13:29 Comment(4)
Thanks alot @Lyngbakr! That is exactly what I need.Blanca
This works to get the lines under control, but unfortunately it puts a white line through the row striping of the table if format="latex". It doesn't put a line through the striping if booktabs=TRUE, but you lose control of the lines. Maybe we kick this upstairs to github for kableExtra?Rachellerachis
@MarkNeal Yeah, a feature request for kableExtra is a good idea.Lenorelenox
The problem with this solution is that when I add another table below the initial one, the 2nd table loses all the horizontal and vertical lines, even in the header rows.Cameroncameroon
B
2

Maybe that wasn't available at the time, but you can now remove horizontal lines in booktab kables using the toprule, midrule, and bottomrule arguments (see https://bookdown.org/yihui/rmarkdown-cookbook/kable.html#customize-latex-tables, section "10.1.10 Customize LaTeX tables").

I don't fully understand how the three parameters work, but setting them all to an empty string removes all default horizontal lines.

table.tbl <- tibble(var1 = c("entry 1", "entry 2", "entry 3", "entry 4"),
                var2 = c(2000, 1000, 3000, 200),
                var3 = c(3000, 2000, 4000, 100))

table.tbl %>% 
  kable("latex", 
        booktabs = T,
        bottomrule = '',
        toprule = '',
        midrule = '') 

Starting from that blank slate, you can then add individual horizontal lines on any given row using row_spec and hline_after as suggested in the OP's post. For example, to add an horizontal line after the penultimate row only:

table.tbl %>% 
  kable("latex", 
        booktabs = T,
        bottomrule = '',
        toprule = '',
        midrule = '')  %>%
  row_spec((table.tbl %>% nrow()-1), hline_after = T)

enter image description here

Brahear answered 2/11, 2022 at 14:56 Comment(0)
B
1

This trick also works with native markdown table such as

\arrayrulecolor{white}

    ||
    |--:| 
    |Cool left aligned quote | 
    | | 
Bosket answered 21/10, 2020 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.