R:渲染 xtable

R: rendering xtable

我有一个 .Rmd 文件包含:

```{r, echo=FALSE, message=FALSE, results='asis'}
library(xtable)
print(xtable(groupGrundALL))  
```  

它使用 RStudio 中的 "Knit Word" 按钮创建并打开一个 Word 文件,但只显示以下文本行而不是预期的(呈现的)table 本身:

% latex table 由 xtable 1.7-4 包 % Wed Oct 2111:14:28 2015

在 R 3.2.2 中生成

当我 运行 在控制台中...

library(xtable)
print(xtable(groupGrundALL)) 

我得到 LaTeX 代码:

% latex table generated in R 3.2.2 by xtable 1.7-4 package
% Wed Oct 21 11:16:48 2015
\begin{table}[ht]
\centering
\begin{tabular}{rlrrr}
\hline
& Retouren.Grund & Wert & Menge & Anzahl \ 
\hline
1 & Fehlbestellung & 685395.00 & 11469.00 & 222 \ 
2 & andere & 237581.00 & 4354.00 & 179 \ 
3 & Abgelaufene Ware & 129780.00 & 3522.00 & 1077 \ 
4 & beschädigte Ware & 37417.00 & 729.00 & 143 \ 
5 & Falschlieferung & 9943.00 & 280.00 &  14 \ 
6 & nicht abgeholt & 1471.00 & 21.00 &  11 \ 
7 & weggezogen & 25.00 & 1.00 &   1 \ 
\hline
\end{tabular}
\end{table}

如何让 table 本身呈现并显示在 Word 文档中?

非常感谢您的帮助!

据我所知,xtable 仅支持 HTML 和 LaTeX 格式(LaTeX 是默认格式)。如果您将文档呈现为 Word 文件,则需要以降价格式传递表格。至于现在要做什么的选项,您可以考虑以下几个选项(以适合您的降价文档的代码形式呈现):

如果编织到 Word 文档:

---
title: "Sample Document"
output: word_document
---

```{r}
groupGrundALL <- 
  structure(list(Retouren.Grund = structure(c(5L, 2L, 1L, 3L, 4L, 
6L, 7L), .Label = c("Abgelaufene Ware", "andere", "beschadigte Ware", 
"Falschlieferung", "Fehlbestellung", "nicht abgeholt", "weggezogen"
), class = "factor"), Wert = c(685395, 237581, 129780, 37417, 
9943, 1471, 25), Menge = c(11469, 4354, 3522, 729, 280, 21, 1
), Anzahl = c(222, 179, 1077, 143, 14, 11, 1)), .Names = c("Retouren.Grund", 
"Wert", "Menge", "Anzahl"), row.names = c(NA, -7L), class = "data.frame")
```

## `knitr::kable` 
```{r, echo=FALSE, message=FALSE, results='asis'}
knitr::kable(groupGrundALL, format = "markdown")
```  

## `pixiedust`
For markdown tables, `pixiedust` is an extended wrapper for `knitr::kable` that allows you to do some additional formatting without having to preprocess data.

```{r, warning = FALSE}
library(pixiedust)
dust(groupGrundALL) %>%
  sprinkle_print_method("markdown")
```

如果您愿意从 GitHub 安装软件包,您也可以使用 Grmd 软件包 (devtools::install_github("gforge/Grmd")) 并编织成 docx_document,这样您就可以使用 xtablekablepixiedust 的 HTML 输出。这意味着您还可以使用 xtablepixiedust 的所有自定义设置。文档完成后,将另存为 HTML 文件,因此您可以右键单击并作为 word 文档打开,或者将扩展名更改为 .docx

---
title: "Sample Document 2"
output: Grmd::docx_document
---


```{r}
groupGrundALL <- 
  structure(list(Retouren.Grund = structure(c(5L, 2L, 1L, 3L, 4L, 
6L, 7L), .Label = c("Abgelaufene Ware", "andere", "beschadigte Ware", 
"Falschlieferung", "Fehlbestellung", "nicht abgeholt", "weggezogen"
), class = "factor"), Wert = c(685395, 237581, 129780, 37417, 
9943, 1471, 25), Menge = c(11469, 4354, 3522, 729, 280, 21, 1
), Anzahl = c(222, 179, 1077, 143, 14, 11, 1)), .Names = c("Retouren.Grund", 
"Wert", "Menge", "Anzahl"), row.names = c(NA, -7L), class = "data.frame")
```

## `xtable` with HTML
```{r, echo=FALSE, message=FALSE, results='asis'}
library(xtable)
print(xtable(groupGrundALL), type = "html")  
```  

## `knitr::kable` 
```{r, echo=FALSE, message=FALSE, results='asis'}
knitr::kable(groupGrundALL, format = "html")
```  

## `pixiedust` with HTML
```{r, warning = FALSE}
library(pixiedust)
dust(groupGrundALL) %>%
  sprinkle_print_method("html")
```

我对 pixiedust 有强烈的偏见(很明显),但是 knitr::kable 可能是处理不需要太多自定义的简单降价表的最快方法。