R降价表格为PDF,与htmlTable包

时间:2017-02-09 22:05:54

标签: r pdf markdown

是否有任何你知道的开发/包可以(通过R markdown)在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我可以按“type”对表进行分组,并将“description”作为多行。我没有设法找到一个包,使我能够创建具有这些特征的该表的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;")

提供: enter image description here

也许不是太棒了但是,对于这个具体情况,足够好:关键的一点是将包装文本放在单元格内。

1 个答案:

答案 0 :(得分:0)

我已尽力使用pixiedust,但似乎我仍然在PDF输出中取得了一些进展。

---
header-includes:
- \usepackage{amssymb}
- \usepackage{arydshln}
- \usepackage{caption}
- \usepackage{graphicx}
- \usepackage{hhline}
- \usepackage{longtable}
- \usepackage{multirow}
- \usepackage[dvipsnames,table]{xcolor}
output:
  pdf_document: default
  html_document: default
---

```{r}
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."))
```

```{r}
# devtools::install_github("nutterb/pixiedust", ref = "new-latex-tables")
library(pixiedust)
library(dplyr)

scenDes <- 
  arrange(scenDes, type)

group_row <- 
  lapply(unique(scenDes$type),
         function(x) which(scenDes$type %in% x))

tbl <- 
  scenDes %>%
  arrange(type) %>%
  dust(float = FALSE)

for (i in seq_along(group_row))
{
  tbl <- sprinkle(tbl,
                  cols = "type",
                  rows = group_row[[i]],
                  merge = TRUE)
}

tbl %>%
  sprinkle(cols = "description",
           width = 3.7,
           width_units = "in") %>%
  medley_all_borders(cols = c("name", "description")) %>%
  sprinkle(cols = "type",
           rows = (1:nrow(scenDes))[c(TRUE, FALSE)],
           border = c("top")) %>%
  sprinkle(cols = "type",
           rows = nrow(scenDes),
           border = "bottom") %>%
  medley_bw()
```

enter image description here