2
votes

I am trying to modify the appearance of a table using kable and kableExtra packages in R Markdown knitting to PDF output.

In particular, I would like to remove all borders from the table and add only one horizontal line under the header and one vertical line next to the row names.

At the moment, I have this code:

---
header-includes:  \setmainfont[Path = C:/Windows/Fonts/]{Arial}
                  \usepackage{colortbl}
                  \arrayrulecolor{white}
output:
  pdf_document:
    latex_engine: xelatex
  word_document: default
---


```{r echo=FALSE, message=FALSE, warning=F, paged.print=TRUE}

library(kableExtra)
library(magrittr)

DATA<- data.frame(jen=c(1,2,3,4,5), feb=c(2,3,4,5,3), mar=c(0,2,4,1,2))
rownames(DATA)<-c("first","second","third","fourth","fifth")


kable(DATA, "latex") 
```

Which generates this table:

Table

To delete all borders, I set default borders to be white as the answer to this question suggested.

Now, I would like to add an horizontal blue line under the header and a vertical blue line at the right side of the row names.

I can manage to get the horizontal line with the following:

---
header-includes:  \setmainfont[Path = C:/Windows/Fonts/]{Arial}
                  \usepackage{colortbl}
                  \arrayrulecolor{white}
output:
  pdf_document:
    latex_engine: xelatex
---


```{r echo=FALSE, message=FALSE, warning=F, paged.print=TRUE}

library(kableExtra)
library(magrittr)

DATA<- data.frame(jen=c(1,2,3,4,5), feb=c(2,3,4,5,3), mar=c(0,2,4,1,2))
rownames(DATA)<-c("first","second","third","fourth","fifth")



kable(DATA, "latex")  %>%
    row_spec(0, extra_latex_after = "\\arrayrulecolor[rgb]{0,.275,.725}") %>%
    row_spec(1:nrow(DATA), extra_latex_after = "\\arrayrulecolor{white}")

```

Obtaining this.

I would like to do the same with a vertical line but there is no such command as 'extra_latex_after' in the function 'column_spec'. It only accepts 'extra_css' commands which obviously do not work for a PDF output.

My goal is to get something like this. I know I can get the same result with other packages for tables, but I am wondering if it is possible to get something like this using kable.

Does anyone know if there is a solution for this?

1
You should show some reproducible code that comes close to what you want, and explain what is wrong with the output. Otherwise, we can't really tell what you are trying to do.user2554330

1 Answers

1
votes

This is indeed not supported so far. You could make a feature request on github.

You can hack this using a regular expression to replace the alignment options in your tabular:

---
title: "Test"
date: 2019-02-13
output: pdf_document
---

```{r header, echo= FALSE, include = T, warning=F}
library(knitr)

tbl <- kable(mtcars[1:4, 1:3], format = "latex")

# here we search for the begin command and its options and replace them
gsub(pattern = "(begin\\{tabular\\})(\\{.*?\\})", 
     repl    = "\\1{l|r !{\\\\color{red}\\\\vrule width 1pt} r|r|}", 
     x       = tbl)
```

enter image description here