R:渲染xtable [英] R: rendering xtable
问题描述
我有一个.rmd文件,其中包含:
I have an .Rmd file containing:
```{r, echo=FALSE, message=FALSE, results='asis'}
library(xtable)
print(xtable(groupGrundALL))
```
使用RStudio中的编织Word"按钮创建并打开一个Word文件,但仅显示以下文本行,而不是预期的(呈现的)表本身:
Using the "Knit Word" button in RStudio it creates and opens a Word file, but only shows the following text line instead of the intended (rendered) table itself:
xtable 1.7-4软件包在R 3.2.2中生成的%乳胶表%2015年10月2日星期三:11:14:28
% latex table generated in R 3.2.2 by xtable 1.7-4 package % Wed Oct 2111:14:28 2015
当我在控制台中运行时...
When I run in the console...
library(xtable)
print(xtable(groupGrundALL))
我得到了LaTeX代码:
I get LaTeX code:
% 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文档中?
How can I get the table itself rendered and shown in the Word document?
非常感谢您的帮助!
推荐答案
据我所知,xtable
仅支持HTML和LaTeX格式(默认为LaTeX).如果要将文档呈现为Word文件,则需要以markdown格式传递表格.至于现在该做什么的选项,您可以考虑以下几种方法(以适合您的降价文档的代码形式显示):
So far as I know, xtable
only supports HTML and LaTeX formats (with LaTeX being the default). If you're rendering your document to a Word file, you need to pass your tables in markdown format. As far as options for what to do now, here are a few you can consider (presented as code suitable for your markdown document):
如果要编织到Word文档:
If knitting to a Word document:
---
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
,pixiedust
.这意味着您还可以使用xtable
和pixiedust
的所有自定义项.文档完成后,将其另存为HTML文件,因此您可以右键单击并以Word文档形式打开,也可以将扩展名更改为.docx
If you're comfortable installing packages from GitHub, you can also use the Grmd
package (devtools::install_github("gforge/Grmd")
) and knit to a docx_document
, which allows you to use the HTML output from xtable
, kable
, and pixiedust
. This means you can also have all the customizations of xtable
and pixiedust
available to you. When the document is completed, it is saved as an HTML file, so you can either right click and open as a word document, or change the extension to .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表的最快方法.
I have a strong bias for pixiedust
(obviously), but knitr::kable
is probably the fastest way to deal with simple markdown tables that don't need much customization.
这篇关于R:渲染xtable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!