【问题标题】:Control text wrapping and hyphenation in kable tables控制 kable 表中的文本换行和断字
【发布时间】:2022-12-23 05:56:35
【问题描述】:

我有一个带有嵌入式表格的 Rmarkdown 文档,但我无法理解表格内容的文本换行和连字符的基本规则。通过 stackoverflow 和其他资源进行搜索并没有提供很多见解。

下面提供了一个示例,仅在示例中指定的列宽是重现我在真实表格中遇到的问题所必需的。经过一些试验和错误后,我能够通过将最后一列标题输入为“ Manufacturer ”来使用连字符,但是这个技巧在该标题下方的行中不起作用。单元格中的文本被截断或溢出到相邻单元格的问题的其他示例显示在第三列(结果)中,单元格条目的格式显示在第二列中。我在第三列和第四列之间添加了边框以突出显示问题。真实表格有 8 列,我在保持可读性的同时尽可能地调整了这些列的宽度。

---
title: 'Table_7_problem'
fontsize: 11pt
output:
  bookdown::pdf_document2:
   toc: false
   number_sections: false 
   latex_engine: xelatex
tables: yes
header-includes:
- \usepackage{booktabs}
- \usepackage{longtable}
- \usepackage{colortbl} # to set row stripe colors
- \usepackage{tabu}
- \setlength{\tabcolsep}{1pt} 
---
```

```{r setup, echo = TRUE, cache = FALSE, warning = FALSE, message = FALSE}
{r setup, echo = FALSE, cache = FALSE, warning = FALSE, message = FALSE}

library(knitr)

```

# Table 7: Appliance durability

This table contains fictional data.

```{r table7, echo = FALSE, cache = FALSE, warning = FALSE, message = FALSE}
{r table7, echo = FALSE, cache = FALSE, warning = FALSE, message = FALSE}

table7 <- data.frame(
  Column_1 = c('Very long string #1 that requires a wide column to accomodate and maintain readability' ,'Very long string #2... and more of the same down rows for this column...','Very long string #3','Very long string #4','Very long string #5','Very long string #6', 'Very long string #7'),
  Column_2 = c('"SampleText"',
               '"Sample Text"',
               '" SampleText"',
               '"SampleText "',
               '" SampleText "',
               '"SampleText #2"',
               '"Sample Text #2"'),
  Column_3 = c('SampleText',
               'Sample Text',
               ' SampleText',
               'SampleText ',
               ' SampleText ',
               'SampleText #2',
               'Sample Text #2"'),
  Column_4 = c('Manufacturer', 
               ' Manufacturer', 
               'Manufacturer ',
               ' Manufacturer ',
               ' LongManufacturerName',
               'Long_Manufacturer_Name',
               "Long Manufacturer Name")
)

###

colnames(table7) <- c("Name", "Cell Content Format", "Result", " Manufacturer ")

library(kableExtra)

  table7 %>% 
  kbl(longtable = TRUE, align = "lllc", booktabs = TRUE) %>% 
  kable_styling(full_width = FALSE, font_size = 8, latex_options = c("repeat_header", "striped"), stripe_color = "gray!15", repeat_header_text = "Table 7 \\textit{continued...}") %>%
  row_spec(0, bold = TRUE) %>% 
  column_spec(1, width = "1.5in") %>%
  column_spec(2, width = "3.825in") %>%
  column_spec(3, width = "0.5in") %>%
  column_spec(4, width = "0.45in", border_left = TRUE)
```

上面的代码产生了这个:

关于如何控制连字符和自动换行以解决这些问题的任何建议或解决方案?

*** 更新 2022-09-07

更新状态——我已经探索了几个用于制作表格的包,到目前为止,没有一个能满足我的所有需求,但对我来说,flextable 包似乎能满足我的大部分需求。更新后的代码和pdf结果如下所示。它可能不漂亮,但可以完成工作。似乎在传递格式化命令时会出现一些冲突,但如果一次输入一个,它们似乎工作得很好,这就是为什么有多个 t7 &lt;-... 语句(我尝试了更复杂的格式和使用单个语句的相同策略工作)。


table7 <- data.frame(
  Column_1 = c('Very long string #1 that requires a wide column to accomodate and maintain readability' ,'Very long string #2... and more of the same down rows for this column...','Very long string #3','Very long string #4','Very long string #5','Very long string #6', 'Very long string #7'),
  Column_2 = c('"SampleText"',
               '"Sample Text"',
               '" SampleText"',
               '"SampleText "',
               '" SampleText "',
               '"SampleText #2"',
               '"Sample Text #2"'),
  Column_3 = c('SampleText',
               'Sample Text',
               ' SampleText',
               'SampleText ',
               ' SampleText ',
               'SampleText #2',
               'Sample Text #2"'),
  Column_4 = c('Manufacturer', 
               ' Manufacturer', 
               'Manufacturer ',
               ' Manufacturer ',
               ' LongManufacturerName',
               'Long_Manufacturer_Name',
               "Long Manufacturer Name")
)

###

colnames(table7) <- c("Name", "Cell Content Format", "Result", "Manu-\nfacturer")

library(flextable)
library(stringr)

set_flextable_defaults(
  font.family = gdtools::match_family(font = "Serif"), 
  font.size = 8,
  padding = 3)

table7$`Manu-\nfacturer` <- str_replace(string = table7$`Manu-\nfacturer`, pattern = 'Manufacturer', replacement = 'Manu-\nfacturer')

t7 <- table7 %>% flextable() %>% 
  width(., width = c(1.5, 3.825, 0.5, 0.45), unit = "in") %>% 
  #add_header_lines(., values = "Table 7") %>% 
  theme_zebra(.)

t7 <- hline(t7, i = 1, border = officer::fp_border(color = "black"), part = "header")
t7 <- flextable::align(t7, i = 1, j = 1, align = "left", part = "header")

t7

以上生成下图。 @Julian 建议的 str_replace 策略实现了断字和换行,flextable 中的 theme_zebra() 保留了行条纹。

【问题讨论】:

    标签: r-markdown kable kableextra flextable hyphenation


    【解决方案1】:

    您可以做的是添加换行符并将 escape = FALSE 添加到您的 kable 函数中。请注意,您还需要转义 #,_ 等。

    table7 <- data.frame(
      Column_1 = c('Very long string 1 that requires a wide column to accomodate and maintain readability' ,'Very long string 2... and more of the same down rows for this column...','Very long string 3','Very long string 4','Very long string 5','Very long string 6', 'Very long string 7'),
      Column_2 = c('"SampleText"',
                   '"Sample Text"',
                   '" SampleText"',
                   '"SampleText "',
                   '" SampleText "',
                   '"SampleText 2"',
                   '"Sample Text 2"'),
      Column_3 = c('Sample
    Text',
                   'Sample
     Text',
                   ' Sample
    Text',
                   'Sample
    Text ',
                   ' Sample
    Text ',
                   'Sample
    Text 2',
                   'Sample 
    Text 2"'),
      Column_4 = c('Manu
    facturer', 
                   ' Manu
    facturer', 
                   'Manu
    facturer ',
                   ' Manu
    facturer ',
                   ' Long
    Manufacturer
    Name',
                   'Long
    Manufacturer
    Name',
                   "Long
     Manufacturer
     Name")
    )
    

    【讨论】:

    • 谢谢(抱歉延迟回复 - 有点网络假期)。我会试试这个。该表是通过从一个非常大的数据库中读取数据而不是手动输入来填充的。
    • 您可以尝试使用stackoverflow.com/questions/57044895/… 将其自动化
    • 感谢您的建议和链接。我认为这些答案的某种组合可以解决问题。我确实很好奇,适用于列标题的格式对该列中的单元格内容没有影响。
    • 我在这里取得了一些进展,但@Julian 的回答中还显示了另一个问题,即:当插入换行符时,行条带化变得不一致/消失......
    • 如果这是您的选择,您可以关闭行条带化。
    猜你喜欢
    • 2015-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2011-04-05
    相关资源
    最近更新 更多