Is there a way to make a kable without lines/borde

2019-07-31 13:51发布

问题:

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)

回答1:

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,