Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)
like image 206
Allan A Avatar asked Dec 20 '18 12:12

Allan A


People also ask

What is knitr kable?

The kable() function in knitr is a very simple table generator, and is simple by design. It only generates tables for strictly rectangular data such as matrices and data frames. You cannot heavily format the table cells or merge cells.

What is Kable?

Kable is a 3PL Provider Who Helps E-Retailers, Subscription Box Companies, and Consumer Brands Succeed Wherever You Sell-Your Website, Amazon, or Online Marketplaces.

How do you insert a table in R markdown?

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd or R document and select “Addins –> Insert Table”.


1 Answers

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

like image 180
Lyngbakr Avatar answered Oct 06 '22 01:10

Lyngbakr