【发布时间】:2017-02-09 22:05:54
【问题描述】:
您是否知道任何开发/包可以(通过 R 降价)在 PDF 文档中创建表格,就像我们能够使用 htmlTable 包创建的 HTML 输出一样(非常灵活!)?
我追求的是结合能力:
- 多行单元格(ztable 无法做到这一点,当您的文本应该适合一个单元格时,这一点至关重要)
- 分组/部分(pander 不能这样做)
- 列和行跨度(pander 不能这样做)
- 旋转列标题(pander 不能这样做)
- 条件格式
我想我尝试了所有方法(Pandoc、knitr、kable、xtable、ztable 等等),但总有一种方法我总是遗漏上述元素之一(Pandoc 通常是我的首选折衷方案)。
感谢您的帮助!
例如,考虑以下 data.frame(在行和列方面,它是一个更大的表的简化版本):
scenDes <- data.frame(type = c("Hist", "Stress", "Hist", "Stress"),
name = c("Equity Markets Rebound in 2009",
"Greece Financial Crisis 2015",
"Libya Oil Shock Feb 2011",
"Russian Financial Crisis 2008"),
description = c("Global equity markets rebound following 2008 drawdown. Use Historical risk factor returns.",
"Athens's resistance via referendum and ultimately agreement to rush through long-resisted economic reforms, imposed by its creditors, in a bid to stay in the eurozone. Use Historical risk factor returns.",
"Civil war in Libya breaks out on February 15th 2011, causing oil prices to surge. Use Historical risk factor returns.",
"War with Georgia and rapidly declining oil prices raise fears of an economic recession within the region. Use Historical risk factor returns."))
使用 htmlTable 我可以按“类型”对表格进行分组,并将“描述”作为多行。我没有设法找到一个包,使我能够创建具有这些特征的该表的 PDF 版本。 理想情况下,出于其他目的,我希望能够添加跨列/行并添加颜色/行以用于条件格式或在一张纸上更好地可视化。
就 htmlTable 而言,我会这样做:
scenDes <- scenDes[order(scenDes$type),]
acCount <- as.vector(table(scenDes$type))
htmlTable(scenDes[,-1],
rnames = FALSE,
align="ll",
rgroup = unique(scenDes$type),
n.rgroup = acCount,
css.cell = "font-size: 9pt;padding-left:1em; padding-right:2em;")
也许不是很好,但对于这种特定情况,已经足够好了:关键是在单元格中包含换行文本。
【问题讨论】:
-
一个最小的可重现示例 - 一个包含所有这些功能的小表以及如何使用
htmlTable对其进行编码 - 会使这个问题变得更好。 -
快速更新:在我看来,以 PDF 格式呈现的表格具有与 HTML 案例相同的灵活性的一般问题仍然存在。您可以将 KableExtra 作为一个包添加到组合中,这可能会有所帮助。