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

3

6

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.

Punchdrunk answered 20/3, 2018 at 16:36 Comment(0)
A
6

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!

Andrus answered 12/6, 2018 at 6:58 Comment(0)
O
2

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

Orleans answered 12/7, 2018 at 13:31 Comment(0)
A
1

Can't provide a full answer, but I was able to suppress the horizontal lines by altering the CSS for the table in the row_spec() function.

dt <- mtcars[1:5, 1:6]
kable(dt, "html") %>%
kable_styling(full_width = FALSE, position = "left") %>%
row_spec(1:4, extra_css = "border-bottom-style: none")

Though this doesn't seem to do anything when included in a Rmarkdown document, it does work for Quarto documents.

enter image description here

Avar answered 14/4, 2023 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.