2

我最近尝试将 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
```

创建文档时完全没有问题,一切似乎都运行顺利......

我已经研究了一段时间,我就是不知道出了什么问题。有人带领吗?

提前致谢

4

3 回答 3

0

在加载 kableExtra 包之前,请设置:

options(kableExtra.auto_format = FALSE)

然后,您的表格应显示在呈现的 Word 文档中。(从这里获取的建议)

于 2021-09-10T09:46:25.277 回答
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
```

输出文档如下所示:

在此处输入图像描述

于 2020-03-09T21:09:20.530 回答
0

感谢您进行编辑以使示例可重现@Mathieu -

所以我看到的一些事情正在发生。

  1. 您正在输出到 Word 但想要使用 html 功能。Word 不是为具有 HTML 功能(如“悬停”、“条纹”等)而设计的。
  2. 您正在尝试kable从另一个表创建者的表创建者创建一个表summary_table。如果您查看summary_table,它实际上会创建一个 LaTeX 标记表。因此,从一种表格形式转换为另一种表格形式会出现问题。

我测试了它,有三个选项:

创建word文档,但是使用qwarps2创建表格

---
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
```

不做任何更改并输出 HTML 文档

---
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)
```

尝试不同的方法,不要使用 qwarps2

为什么不knitr直接从数据中创建表呢?即不使用qwraps2?要获得与 qwarps2 类似的功能,请尝试使用carpenter包(免责声明,我创建了它,但如果它不起作用,则在 README/首页末尾有一个类似包的列表)。您可以将数据以您想要的方式获取表格,然后最后使用 knitr 从经过整理的数据创建一个表格。

于 2019-03-14T10:20:01.547 回答