这是您生成 pdf 格式表格的部分问题的解决方案。您需要在xtable 中调整表格的样式,以获得所需的斑马条纹和合并的列。一般来说,从 html 到 pdf 的转换并不是那么简单,所以更好的解决方案是首先使用 LaTeX 生成表格(我知道你不想要 LaTeX,但至少这是用 R 和 @ 编译成 pdf 987654324@辛苦了):
library(xtable)
dt <- mtcars[1:5, 1:4]
filename <- tempfile(fileext = ".tex")
capture.output(print(xtable(dt)), file = filename)
foo <- readLines(filename)
writeLines(c("\\documentclass[hidelinks]{article}",
"\\begin{document}",
foo,
"\\end{document}"),
con = filename)
tools::texi2dvi(filename, pdf = TRUE)
您应该查看https://cran.r-project.org/web/packages/xtable/vignettes/xtableGallery.pdf 以获得您想要的样式。祝你好运。
编辑:
看来你也可以使用kabelExtra:
library(knitr)
library(kableExtra)
dt <- mtcars[1:5, 1:4]
# LaTeX table
a <- kable(dt, format = "latex", caption = "Demo Table") %>%
kable_styling(bootstrap_options = "striped",
full_width = F) %>%
add_header_above(c(" ", "Group 1" = 2, "Group 2[note]" = 2)) %>%
add_footnote(c("table footnote"))
filename <- tempfile(fileext = ".tex")
capture.output(a, file = filename)
foo <- readLines(filename)
writeLines(c("\\documentclass[hidelinks]{article}",
"\\begin{document}",
foo,
"\\end{document}"),
con = filename)
tools::texi2dvi(filename, pdf = TRUE)