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)
Allan A
  • 427
  • 8
  • 17

3 Answers3

8

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

Dan
  • 11,370
  • 4
  • 43
  • 68
  • Thanks alot @Lyngbakr! That is exactly what I need. – Allan A Dec 20 '18 at 13:41
  • 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? – Mark Neal Mar 18 '20 at 01:47
  • @MarkNeal Yeah, a feature request for `kableExtra` is a good idea. – Dan Mar 18 '20 at 17:07
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

FZS
  • 243
  • 1
  • 8
1

This trick also works with native markdown table such as

\arrayrulecolor{white}

    ||
    |--:| 
    |Cool left aligned quote | 
    | |