R:渲染xtable

时间:2015-10-21 09:40:57

标签: r knitr xtable

我有一个包含以下内容的.Rmd文件:

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

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

%乳胶表在R 3.2.2中由xtable 1.7-4包装生成%Wed 2111:2015年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文档中?

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:6)

据我所知,xtable仅支持HTML和LaTeX格式(默认使用LaTeX)。如果要将文档呈现为Word文件,则需要以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,这样您就可以使用来自xtablekablepixiedust。这意味着您还可以使用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可能是处理不需要太多自定义的简单降价表的最快方法。