簡體   English   中英

R降價表到PDF與htmlTable包一樣靈活

[英]R markdown tables to PDF as flexible as htmlTable package

是否有任何你知道的開發/包可以(通過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我可以按“類型”對表進行分組,並將“描述”作為多行。 我沒有設法找到一個包,使我能夠創建具有這些特征的該表的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;")

提供這個: 在此輸入圖像描述

也許不是很棒但是,對於這個特定的情況,足夠好:關鍵的一點是將包裝文本放在單元格內。

我已經用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()
```

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM