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

断了今生、忘了曾经 提交于 2020-05-15 12:27:43

问题


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.


回答1:


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




回答2:


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!



来源:https://stackoverflow.com/questions/49389646/is-it-possible-to-remove-horizontal-lines-in-kable-kableextra-with-html-option

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!