Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to remove horizontal lines in Kable kableextra with html option?

I am trying to create a table using kable/kableextra without showing the horizontal lines in the table except for the first row which is the row names.

```
{r echo=FALSE}
library(knitr)
library(kableExtra)
options(knitr.kable.NA = '')
dt <- mtcars[1:5, 1:6]
kable(dt, "html") %>%
  kable_styling(full_width = F, position = "left") %>%
  row_spec(0, align = "c",bold=T ) %>%
  column_spec(1, bold = T)
```

In the code above there is a line below the first row, which I like since those are row names, but there are lines between every row which I would like to remove.

Ideally I would like to have a slightly thicker line at the top at bottom of this table. Similar to the booktabs look in LaTeX.

I have read the documentation but the CSS is beyond me.

Thanks for any suggestions.

like image 796
Stephen Lien Avatar asked Mar 20 '18 16:03

Stephen Lien


2 Answers

You can include a LaTeX table in your html doc as an image, but you need a complete LaTeX distribution (not tinytex) and the R package magick (+Ghostscript if you are on Windows).

Replace

kable(dt, "html") %>%

with

kable(dt, "latex", booktabs=T) %>%

and add

  kable_as_image()

as last line (dont forget the pipe symbol). The following code works for me:

```{r echo=FALSE}
library(knitr)
library(kableExtra)
options(knitr.kable.NA = '')
dt <- mtcars[1:5, 1:6]
kable(dt, "latex", booktabs=T) %>%
  kable_styling(full_width = F, position = "left") %>%
  row_spec(0, align = "c",bold=T ) %>%
  column_spec(1, bold = T) %>%
  kable_as_image()
```

Ref: See page 24 here:
https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_pdf.pdf

like image 20
Dan Boucher Avatar answered Sep 22 '22 11:09

Dan Boucher


What you need is to set booktabs = T argument inside kable. In your example, just change the following line of code:

kable(dt, "html") 

to:

kable(dt, "html", booktabs = T)

Cheers!

like image 145
LunaSare Avatar answered Sep 22 '22 11:09

LunaSare