打印表

有几个包允许以 HTML 或 LaTeX 表的形式输出数据结构。它们的灵活性大多不同。

在这里我使用包:

  • knitr
  • xtable
  • 拉皮条

对于 HTML 文档

author: "Martin Schmelzer"
date: "29 Juli 2016"
output: html_document
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
`library(knitr)`
`library(xtable)`
`library(pander)`
df <- mtcars[1:4,1:4]
```

# Print tables using `kable`
```{r, 'kable'}
`kable(df)`
```

# Print tables using `xtable`
```{r, 'xtable', results='asis'}
print(`xtable(df)`, type="html")
```

# Print tables using `pander`
```{r, 'pander'}
`pander(df)`
```

StackOverflow 文档

对于 PDF 文档

author: "Martin Schmelzer"
date: "29 Juli 2016"
output: pdf_document
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
`library(knitr)`
`library(xtable)`
`library(pander)`
df <- mtcars[1:4,1:4]
```

# Print tables using `kable`
```{r, 'kable'}
`kable(df)`
```

# Print tables using `xtable`
```{r, 'xtable', results='asis'}
print(xtable(df, caption="My Table"))
```

# Print tables using `pander`
```{r, 'pander'}
`pander(df)`
```

StackOverflow 文档

如何在每张表之前停止 xtable 打印注释?

options(xtable.comment = FALSE)