RMarkdown ztable的pdf_output | xtable | HTMLTABLE

时间:2016-06-14 13:22:51

标签: r r-markdown

我使用ztable,xtable或htmlTable包成功生成表,没有任何问题。但是,我想知道这些表是否可以作为pdf文档输出。当我尝试编织pdf_output时,不会显示该表。相反,信息显示为多个字符串。我试过改变乳胶引擎(使用ztable)和其他方法没有运气。

我看过这里:
http://www.mzan.com/article/29773068-rmarkdown-latex-table-output-difficulties.shtml

我也看过小插曲等等。

示例(RMarkdown):

---  
output: pdf_document
---

```{r, message = F, results = 'asis'} 
# will throw out Error: pandoc document conversion failed with error 43
library(ztable)
data(iris)
options(ztable.type="latex") 
zt <- ztable(iris[1:5,], caption = "ztable")
zt <- addcgroup(zt,
                cgroup = c("group 1", "group 2"),
                n.cgroup = c(2,3))
print(zt)
```  

```{r, message = F }
# since it's html, will produce text only
library(htmlTable)
data(mtcars)
colnames(mtcars) <- NULL
htmlTable(mtcars[1:5,], caption = "htmlTable",
          cgroup = c("group 1", "group 2"),
          n.cgroup = c(5,6))
```

1 个答案:

答案 0 :(得分:5)

添加header-includes: \usepackage{colortbl}可解决ztable的错误。我不认为htmlTable有乳胶引擎。

完整代码:

---  
output: pdf_document
header-includes: \usepackage{colortbl}
---

```{r, message = F, results = 'asis'} 
library(ztable)
data(iris)
options(ztable.type="latex") 
zt <- ztable(iris[1:5,], caption = "ztable")
zt <- addcgroup(zt,
                cgroup = c("group 1", "group 2"),
                n.cgroup = c(2,3))
print(zt)
```  

```{r, message = F }
# since it's html, will produce text only
library(htmlTable)
data(mtcars)
colnames(mtcars) <- NULL
htmlTable(mtcars[1:5,], caption = "htmlTable",
          cgroup = c("group 1", "group 2"),
          n.cgroup = c(5,6))
```