R: rendering xtable

落爺英雄遲暮 提交于 2019-12-20 02:43:09

问题


I have an .Rmd file containing:

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

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:

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

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}

How can I get the table itself rendered and shown in the Word document?

Thanks a lot for your help!


回答1:


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

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

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

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.



来源:https://stackoverflow.com/questions/33255836/r-rendering-xtable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!