我有一个 .Rmd 文件包含:
```{r, echo=FALSE, message=FALSE, results='asis'}
library(xtable)
print(xtable(groupGrundALL))
```
它使用 RStudio 中的“Knit Word”按钮创建并打开一个 Word 文件,但只显示以下文本行而不是预期的(呈现的)表格本身:
xtable 1.7-4 包在 R 3.2.2 中生成的 % latex 表 % 2015 年 10 月 21 日星期三:14:28
当我在控制台中运行时...
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}
如何让表格本身呈现并显示在 Word 文档中?
非常感谢您的帮助!
最佳答案
据我所知,xtable
仅支持 HTML 和 LaTeX 格式(LaTeX 是默认格式)。如果您将文档呈现为 Word 文件,则需要以 Markdown 格式传递表格。至于现在要做什么的选项,您可以考虑以下几个选项(以适合您的 Markdown 文档的代码形式呈现):
如果编织到 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
,它允许您使用 xtable
、kable
和 pixiedust
的 HTML 输出。这意味着您还可以使用 xtable
和 pixiedust
的所有自定义项。文档完成后,将保存为 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
可能是处理不需要太多的简单 Markdown 表的最快方法定制。
关于R:渲染 xtable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33255836/