我最近尝试将 kable 包用于表格,虽然我对在 R 脚本中获得的结果非常满意,但我不知道如何在 R Markdown 文件中使用它们。
这是一个在 R 脚本中工作的表格的简短示例,但当我尝试在 Markdown 文档中复制它时不会。
data(mtcars)
mtcars
## @knitr install
check_and_install <- function( packname ) { # given package name, check installation, install if not found
if ( packname %in% rownames(installed.packages()) == FALSE ) {
install.packages( packname, repos="http://cran.rstudio.com/" )
}
}
check_and_install("kableExtra")
check_and_install("dplyr")
check_and_install("qwraps2")
check_and_install("reprex")
library(kableExtra)
library(dplyr)
library(qwraps2)
library(reprex)
## Tableau
summary_test <-
list("Cylindres" =
list("Huit" = ~ qwraps2::n_perc0(cyl == 8,show_symbol=TRUE),
"Six" = ~ qwraps2::n_perc0(cyl == 6,show_symbol=TRUE),
"Quatre" = ~ qwraps2::n_perc0(cyl == 4,show_symbol=TRUE)),
"Vitesses" =
list("Cinq" = ~ qwraps2::n_perc0(gear == 5,show_symbol=TRUE),
"Quatre" = ~ qwraps2::n_perc0(gear == 4,show_symbol=TRUE),
"Trois" = ~ qwraps2::n_perc0(gear == 3,show_symbol=TRUE))
)
tabtest2<-summary_table(dplyr::group_by(mtcars, am), summary_test)
kable_out <- kable(tabtest2, format = "html", caption = "", col.names=c("Auto","Manuelle"), booktabs = T, full_width = F) %>%
kable_styling(bootstrap_options = c("striped", "hover")) %>%
kableExtra::group_rows("Cylindres", 1, 3) %>%
kableExtra::group_rows("Vitesses", 4, 6)
kable_out
现在至于下一步,我想将此选项卡包含在 R-markdown 文档中,理想情况下输出将是一个 word 文件。这就是我遇到问题的地方:我找不到正确包含它的方法。Markdown 完全打乱了单词输出中的格式。请注意,如果我将输出切换为 HTML,则没有问题……不幸的是,我现在必须提供一个 word 文档,所以这不是一个选项。
---
title: "Test2"
author: "MJ"
date: "14 mars 2019"
output: word_document
always_allow_html: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(cache = FALSE, include = FALSE)
library(knitr)
opts_chunk$set(echo = FALSE, cache=FALSE)
read_chunk('C:/Users/Mathieu/Desktop/indicateurs pps/Test SO.R')
```
```{r install, include=FALSE}
```
## Analyse comparative
```{r table1, include=T}
knitr::kable(tabtest2, format = "html", caption = "", col.names=c("Auto","Manuelle"), booktabs = T, full_width = F) %>%
kable_styling(bootstrap_options = c("striped", "hover")) %>%
kableExtra::group_rows("Cylindres", 1, 3) %>%
kableExtra::group_rows("Vitesses", 4, 6)
kable_out
```
创建文档时完全没有问题,一切似乎都运行顺利......
我已经研究了一段时间,我就是不知道出了什么问题。有人带领吗?
提前致谢