【发布时间】:2018-07-09 13:50:13
【问题描述】:
考虑这个例子
---
title: "R Notebook"
output:
pdf_document: default
---
Hello this is a nice test
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
options(knitr.table.format = "latex")
library(knitr)
library(kableExtra)
library(dplyr)
# needed
kable(cars, "latex") %>% kable_styling(bootstrap_options = "striped")
```
here we go
```{r, results = "asis"}
for (threshold in c(20, 25)) {
cat("\n\n\\pagebreak\n")
x <- mtcars %>%
filter(mpg < threshold) %>%
mutate(disp = cell_spec(
disp, "latex", color = "white", bold = T,
background = spec_color(1:10, end = 0.9, option = "A")
)) %>%
kable('latex', booktabs = T, escape=F) %>%
kable_styling(latex_options = c("striped", "hold_position"),
full_width = T) %>%
column_spec(1, bold = T, color = "red") %>%
add_header_above(c(" ", "$\\\\beta$" = 10), escape = F)
cat(x)
}
```
我正在尝试使用此处解释的技巧how to use a for loop in rmarkdown? 在我的 pdf 文件中使用 kableExtra 中的所有原始功能。
这是一个很好的例子,说明如何生成一个在 for 循环中创建表格的动态 rmarkdown 文档。
确实不错,除了包含一些神秘空白的cell_specs 渐变...
有什么想法吗?
谢谢!
【问题讨论】:
-
在one of the examples for
kableExtra中,它使用了kable("latex", escape=F, ...),这样可以吗? -
哈!!这样可行!也许您可以将其发布为 aswer 并添加一些不错的技巧?
-
喜欢带有数学公式的标题?
-
我注意到您正在尝试同时呈现 pdf 和 html。我会建议你从
kable和cell_spec中删除"latex"。kableExtra0.9.0 会自动设置格式。
标签: r r-markdown kable kableextra