我最近尝试将kable包用于表,尽管我对R脚本获得的结果感到非常满意,但我不知道如何在R Markdown文件中使用它们。 / p>
这是一个可以在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
```
创建文档时完全没有问题,一切似乎都顺利运行...
我已经研究了一段时间,但我根本不知道出了什么问题。有谁带领某人?
预先感谢
答案 0 :(得分:0)
感谢所做的编辑,使示例可以重现@Mathieu-
所以我看到的几件事正在发生。
kable
的表创建者summary_table
创建一个表。如果您查看summary_table
,它实际上会创建一个LaTeX标记表。因此,从一种表格形式转换为另一种表格形式将存在问题。 我对其进行了测试,共有三个选项:
---
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("Test SO.R")
options(qwraps2_markup = "markdown") # this is new
```
```{r install, include=FALSE}
```
## Analyse comparative
```{r table1, include=TRUE, results='asis'}
tabtest2
```
---
title: "Test2"
author: "MJ"
date: "14 mars 2019"
output: html_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("Test SO.R")
```
```{r install, include=FALSE}
```
## Analyse comparative
```{r table1, include=TRUE, results='asis'}
knitr::kable(tabtest2, caption = "", format = "html", 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)
```
为什么不直接从数据中直接创建knitr
表?即不使用qwraps2?要获得与qwarps2类似的功能,请尝试使用carpenter软件包(免责声明,我创建了它,但是如果不起作用,则在README /第一页的末尾有一个类似软件包的列表)。您可以按照表的方式获取数据,最后使用knitr从混乱的数据创建表。
答案 1 :(得分:0)
请考虑以下.Rmd文件以获取示例用例和说明。
---
title: "summary table"
output: rmarkdown::word_document
---
To get the summary table from the
`r qwraps2::CRANpkg(qwraps2) `
package to render in a word document you need to *not* pass the return from
`r qwraps2::backtick(summary_test) `
to
`r paste0(qwraps2::backtick(knitr::kabel), ".") `
The reason is that
`r qwraps2::backtick(summary_test) `
returns a character matrix and a
`r qwraps2::backtick(print) `
method is reasponsible for generating either the markdown or LaTeX table.
```{r label = "install_cran", include = FALSE}
# remotes::install_cran is similar to your check_and_install function.
remotes::install_cran("kableExtra")
remotes::install_cran("dplyr")
remotes::install_cran("qwraps2")
```
```{r label = "namespaces"}
library(kableExtra)
library(qwraps2)
```
By default
`r qwraps2::CRANpkg(qwraps2) `
will format results for LaTeX. Set the default markup language to markdown
to change this behavior.
```{r }
options(qwraps2_markup = "markdown")
```
**EDIT:** as of version qwraps2 version 0.5.0 the use of the data pronoun
`.data` is no longer needed nor recommented.
```{r label = "define_summary_test"}
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))
)
```
Finally, build the table and look at the structure of the object.
```{r label = 'tabtest2'}
tabtest2 <- summary_table(mtcars, summary_test, by = "am")
str(tabtest2)
```
Note that the object is a
`r qwraps2::backtick(qwraps2_summary_table) `
is a character matrix with attributes for the row, column, and row group
names. Using the default print method in R we see a character matrix.
```{r }
print.default(tabtest2)
```
Using the print method for
`r qwraps2::backtick(qwraps2_summary_table) `
objects (done impliclity here) gives the markdown:
```{r results = "markup"}
tabtest2
```
To get the table to render nicely use the "asis" value for the results chunk
option:
```{r results = "asis"}
tabtest2
```
输出文档如下: