Is it possible to add vertical lines to tables produced with R knitr::kable in pdf?

痴心易碎 提交于 2021-01-27 07:12:25

问题


I want to produce a table with knitr::kable with vertical lines on the borders and between certain columns. Is there a way to do it? My output document is pdf.

Thanks!


回答1:


Not too much clear, but maybe this could help:

library(knitr)
library(kableExtra)
library(dplyr)

dt <- mtcars[1:5, 1:6]
dt %>% 
kable() %>%
# here you can add the vertical line, in my example, for all the columns
column_spec (1:7,border_left = T, border_right = T) %>%
kable_styling()

And if you need to save it as .pdf:

save_kable(k, "k.pdf")

With k as the result of the code above.




回答2:


Answer using huxtable:

library(huxtable)
library(dplyr)

as_hux(mtcars[1:5, 1:6], add_colnames = TRUE) %>%
      set_right_border(2:5, everywhere, 0.4) %>%
      set_bottom_border(1, everywhere, 0.4)

You can then save it to PDF with quick_pdf(), or print it within a rmarkdown document.



来源:https://stackoverflow.com/questions/56627689/is-it-possible-to-add-vertical-lines-to-tables-produced-with-r-knitrkable-in-p

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