我正尝试使用kable()和kable_styling获取包含条件格式表的pdf文档,如下所述:https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html。
这样做会遇到错误(请参见代码)。似乎是kable()生成的Latex代码是原因。
在此信息后,您可以找到:
用于创建html文件的r降价代码
r降价代码,该代码应创建与HTML代码相同的文档,但应使用.pdf
这是我想在pdf文档中获取的表格:https://i.stack.imgur.com/mojNa.jpg
---
title: "testhtml"
output:
pdf_document:
fig_caption: yes
highlight: tango
number_sections: yes
toc: yes
html_document:
number_sections: yes
theme: united
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
if (!require(tidyverse)) {install.packages("tidyverse")}
library(tidyverse)
if (!require(knitr)) {install.packages("knitr")}
library(knitr)
if (!require(kableExtra)) {install.packages("kableExtra")}
library(kableExtra)
if (!require(tinytex)) {install.packages("tinytex")}
library(tinytex)
#Data sample
risque_alpha = 0.05
synthese_ex <- structure(list(campagne = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "2017", class = "factor"),
prelevement = structure(c(1509667200, 1509667200, 1509667200, 1509667200, 1509667200, 1509667200,
1504483200, 1504483200, 1504483200, 1504483200), class = c("POSIXct", "POSIXt"),
tzone = "UTC"),
profondeur = c("-110", "-90", "-70", "-50", "-30", "-10", "-110", "-90", "-70", "-50"),
test_applique = c("ANOVA","Kruskal-Wallis", "ANOVA", "ANOVA", "ANOVA", "ANOVA", "ANOVA", "Kruskal-Wallis",
"Kruskal-Wallis", "Kruskal-Wallis"),
p_value_test = c(0.2691, 0.16966, 0.29958, 0.25323, 8e-05, 0.00214, 0.70138, 0.45697, 0.69763, 0.65208)),
row.names = 2:11, class = "data.frame")
#conditional formatting
options(knitr.table.format = "html") # ....................... for pdf this causes a prolem!?
tab_synthese <- synthese_ex %>% mutate(p_value_test = cell_spec(p_value_test, color = ifelse(p_value_test < risque_alpha, "green","red"))) %>% kable(escape = FALSE, booktabs = T, linesep = "") %>% kable_styling("striped", full_width = F)
tab_synthese
```
如果将以上Rmd作为html运行,则会得到
Fehler:产生可在文档定位中找到HTML输出的函数 乳胶输出。请将此文档的输出类型更改为HTML。 另外,您可以通过添加以下内容来允许以非HTML格式输出HTML: rmarkdown文件的YAML前端的此选项: always_allow_html:是 但是请注意,HTML输出在非HTML中将不可见 格式。 奥斯福伦·昂古特尔
---
title: "testpdf"
output:
pdf_document:
fig_caption: yes
highlight: tango
number_sections: yes
toc: yes
html_document:
number_sections: yes
theme: united
toc: yes
#always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
if (!require(tidyverse)) {install.packages("tidyverse")}
library(tidyverse)
if (!require(knitr)) {install.packages("knitr")}
library(knitr)
if (!require(kableExtra)) {install.packages("kableExtra")}
library(kableExtra)
if (!require(tinytex)) {install.packages("tinytex")}
library(tinytex)
#Data sample
risque_alpha = 0.05
synthese_ex <- structure(list(campagne = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "2017", class = "factor"),
prelevement = structure(c(1509667200, 1509667200, 1509667200, 1509667200, 1509667200, 1509667200,
1504483200, 1504483200, 1504483200, 1504483200), class = c("POSIXct", "POSIXt"),
tzone = "UTC"),
profondeur = c("-110", "-90", "-70", "-50", "-30", "-10", "-110", "-90", "-70", "-50"),
test_applique = c("ANOVA","Kruskal-Wallis", "ANOVA", "ANOVA", "ANOVA", "ANOVA", "ANOVA", "Kruskal-Wallis",
"Kruskal-Wallis", "Kruskal-Wallis"),
p_value_test = c(0.2691, 0.16966, 0.29958, 0.25323, 8e-05, 0.00214, 0.70138, 0.45697, 0.69763, 0.65208)),
row.names = 2:11, class = "data.frame")
#conditional formatting
options(knitr.table.format = "latex") # ....................... for pdf this causes a prolem!
tab_synthese <- synthese_ex %>% mutate(p_value_test = cell_spec(p_value_test, color = ifelse(p_value_test < risque_alpha, "green","red"))) %>% kable(escape = FALSE, booktabs = T, linesep = "") %>% kable_styling("striped", full_width = F)
tab_synthese
```
问题似乎出在kable_styling()
函数上。请注意,options(knitr.table.format = "")
部分已被删除,并由format = "pandoc"
函数中的kable()
取代。
---
title: "testpdf"
output:
pdf_document:
fig_caption: yes
highlight: tango
number_sections: yes
toc: yes
html_document:
number_sections: yes
theme: united
toc: yes
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
```
```{r}
if (!require(tidyverse)) {install.packages("tidyverse")}
library(tidyverse)
if (!require(knitr)) {install.packages("knitr")}
library(knitr)
if (!require(kableExtra)) {install.packages("kableExtra")}
library(kableExtra)
if (!require(tinytex)) {install.packages("tinytex")}
library(tinytex)
#Data sample
risque_alpha = 0.05
synthese_ex <- structure(list(campagne = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L), .Label = "2017", class = "factor"),
prelevement = structure(c(1509667200, 1509667200, 1509667200, 1509667200, 1509667200, 1509667200,
1504483200, 1504483200, 1504483200, 1504483200), class = c("POSIXct", "POSIXt"),
tzone = "UTC"),
profondeur = c("-110", "-90", "-70", "-50", "-30", "-10", "-110", "-90", "-70", "-50"),
test_applique = c("ANOVA","Kruskal-Wallis", "ANOVA", "ANOVA", "ANOVA", "ANOVA", "ANOVA", "Kruskal-Wallis",
"Kruskal-Wallis", "Kruskal-Wallis"),
p_value_test = c(0.2691, 0.16966, 0.29958, 0.25323, 8e-05, 0.00214, 0.70138, 0.45697, 0.69763, 0.65208)),
row.names = 2:11, class = "data.frame")
#conditional formatting
tab_synthese <- synthese_ex %>% mutate(p_value_test = cell_spec(p_value_test, color = ifelse(p_value_test < risque_alpha, "green","red"))) %>% kable(format = "pandoc", escape = FALSE, booktabs = T, linesep = "") %>% kable_styling( "striped", full_width = F)
tab_synthese
`
``