如何在rmarkdown中的pdf_document中的htmlTable包中呈现表? [英] How to render table from htmlTable package in pdf_document in rmarkdown?

查看:97
本文介绍了如何在rmarkdown中的pdf_document中的htmlTable包中呈现表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在rmarkdown中(在RStudio中),我使用htmlTable包在html文档中生成漂亮的表.现在,我希望在渲染pdf文档时获得相同的结果.表格显示不正确.如何让rmarkdown在pdf文档中生成表格的方式与在html文件中生成表格的方式相同?

Within rmarkdown (in RStudio) I use htmlTable package to generate nice looking tables in my html documents. Now I want the same result when rendering a pdf document. The tables aren't rendered properly. How can I let rmarkdown generate tables in my pdf document the same way it does in my html's?

这是带有表的.Rmd文件的工作示例:

This is a working example of a .Rmd file with a table:

---
title: "test"
output: pdf_document
---

```{r results="asis"}
library(htmlTable)
c1 <- c("test1","test1","test2","test2")
c2 <- c(1,2,3,4)
data_object <- as.data.frame(cbind(c1,c2))
names(data_object) <- c("test","test2")
print(htmlTable(data_object))
```

在RStudio中单击编织pdf.

Click knit pdf in RStudio.

我的pdf文档中的结果是:

The result in my pdf-document is:

test
library(htmlTable)
c1 <- c("test1","test1","test2","test2")
c2 <- c(1,2,3,4)
data_object <- as.data.frame(cbind(c1,c2))
names(data_object) <- c("test","test2")
print(htmlTable(data_object))
test
test2
1
1
1
2
1
2
3
2
3
4
2
4
1

(表部分的)结果应为:

The result (of the table part) should be:

有人知道如何解决这个问题吗?

Does anyone have an idea about how to solve this?

推荐答案

如果要输出PDF,则应将R对象转换为

If you want PDF output, you should transform the R object to

  • LaTeX或
  • 降价

前者具有更多的格式设置选项,但后者更简单,可以进一步转换为HTML或PDF(除了其他格式).因此,您可能想尝试使用kable或更强大的pander软件包,而不是htmlTable软件包:

The prior has a lot more formatting options, but the latter is a lot simpler and can be converted further to either HTML or PDF (besides a bunch of other formats). So instead of the htmlTable package, you might want to give a try to kable or the more robust pander package:

---
title: "test"
output: pdf_document
---

```{r}
library(pander)
data_object <- data.frame(test = paste0('test', 1:4), test2 = 1:4)
pander(data_object)
```

在上述文档上调用rmarkdown::render后,得到以下PDF:

Resulting in the following PDF after calling rmarkdown::render on the above document:

这篇关于如何在rmarkdown中的pdf_document中的htmlTable包中呈现表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆