【问题标题】:table with long text, bullet points and specific table width带有长文本、项目符号和特定表格宽度的表格
【发布时间】:2018-10-24 06:38:06
【问题描述】:

我希望表格在一列中有项目符号并具有特定的表格宽度(以便在呈现为 PDF 时放置在一页上)。

如何在rmarkdown 中使用众多软件包之一来实现这一点?


到目前为止我已经尝试过和拥有的:

---
output: pdf_document
---

```{r, include = FALSE}
df <- data.frame(col1 = "Some really long text here. I mean some reeeeeaaly loooong text. So long, it should be wrapped. Really.",
                 col2 = "* bullet point 1\n * bullet point 2", col3 = "Yes, there is still another column.")
```

# Attempt 1: kableExtra
```{r, echo = FALSE, warning = FALSE}
library(kableExtra)
df1 <- df
df1$col2 <- linebreak(df1$col2)
knitr::kable(df1, escape = FALSE) %>% column_spec(1, width = "15em")
```

# Attempt 2: pander
```{r, echo = FALSE}
pander::pander(df, keep.line.breaks = TRUE, style = 'grid', justify = 'left')
```

这呈现为:

如您所见,这两个选项都有一些注意事项。 kableExtra 版本确实具有适合一页的特定表格宽度,但不能很好地显示项目符号。而pander 解决方案可以很好地呈现项目符号但跨越多个页面,因为我不知道如何在pander 中指定表格宽度。

有没有可以同时兼顾的解决方案?

相关问题例如herethere

【问题讨论】:

    标签: r r-markdown knitr kable pander


    【解决方案1】:

    基于 kableExtra 的替代解决方案(可选:带脚注)

    这种方法允许添加标题、在表格中手动添加脚注以及固定列宽。

    创建项目符号列表:

    • 来自 LaTex 的“厚”\cdots 被用作子弹 (alternatives see here)
    • 使用\n 强制换行(因此缩进不如@daroczig 的pander 方法好)。

    MWE

    library(stringi); library(kableExtra); library(dplyr)
    string_short <- "Descriptor"
    string_long <- substr(stri_rand_lipsum(1), 1, 50)
    
    # add footnotes manually within table
    string_bulletlist <- "$\\boldsymbol{\\cdot}$ bullet point 1: foo$^{a}$ \n $\\boldsymbol{\\cdot}$ bullet point 2: bar$^{b}$"
    
    df <- data.frame(col1 = c(string_short, string_short),
                     col2 = c(string_bulletlist, string_bulletlist),
                     col3 = c(string_long, string_long)
    )
    col_names <- c("Descriptor", "Description with list", "Some comment")
    
    # header: bold column names
    colnames(df) <- paste0("\\textbf{", col_names,"}")
    
    # add footnote with kableExtra commands
    names(df)[1] <- paste0(names(df)[1], footnote_marker_symbol(1))
    
    df %>%
      mutate_all(linebreak) %>% # required for linebreaks to work
      kable(
        "latex",  
        escape = F, 
        booktabs=TRUE, 
        align = "l",
        caption = 'kableTable with bullet list and footnote') %>%
      # kable_styling(full_width = F) %>% # makes no difference here
      footnote(general = "General comment of the table.",
               alphabet = c("Footnote A;", "Footnote B;"),
               symbol = c("Footnote Symbol 1")) %>% 
      column_spec(1, width = "5em") %>% # fix width column 1
      column_spec(2, width = "10em") %>% # fix width column 2
      column_spec(3, width = "15em") # fix width column 3
    

    要[提高行间距[(https://stackoverflow.com/questions/53794142/increase-line-row-spacing-with-kableextra),可以在 Rmd 中的代码块之前和之后添加以下内容:

    \renewcommand{\arraystretch}{1.5} <!-- increase line spacing for the table -->
    RMD CHUNK HERE
    \renewcommand{\arraystretch}{1} <!-- reset row hight/line spacing -->
    

    评论:

    我还尝试了@daroczig 的pander 方法,并取得了以下经验:

    • 专业人士: 漂亮的行距加上“真正的项目符号列表”(与 kableExtra 方法中的 $\boldsymbol{\cdot}$-workaround 相比)。
    • 缺点:项目符号列表后包含一个很大的空格

    此外,当在使用huskydown thesis template 的 Rmd 文件中使用 pander 方法时,脚注会极大地弄乱表格对齐方式。

    【讨论】:

      【解决方案2】:

      使用pandoc.tablesplit.table参数(由pander在后台调用)或通过panderOptionstable.split.table一般禁用分表,例如

      pander::pander(df, keep.line.breaks = TRUE, style = 'grid', justify = 'left', split.table = Inf)
      

      library(pander)
      panderOptions('table.style', 'grid')
      panderOptions('table.alignment.default', 'left')
      panderOptions('table.split.table', Inf)
      panderOptions('keep.line.breaks', TRUE)
      pander(df)
      

      【讨论】:

      • 谢谢,@daroczig。您的解决方案适用于问题中的示例。然而,在我的真实示例中,表格仍然分为两页。你知道为什么这仍然会发生吗?列文本是否太长?
      • @daroczig 有时我必须使用grid tables/pander 表。如果它们包含一个项目符号列表,它们总是在列表之后产生一个空格,就像我编译你的代码一样。你知道这个空白可能来自哪里以及如何避免/摆脱它吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-22
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      • 2021-09-06
      • 1970-01-01
      相关资源
      最近更新 更多