【问题标题】:Modify the format of a portion of a flextable cell修改柔性表格单元格的一部分的格式
【发布时间】:2017-11-30 13:49:12
【问题描述】:

我想修改部分单元格的格式,同时保留未修改的剩余文本以包含在 .docx 报告中:
例如,Brill (Scophthalmus rhombus) 在细分 22-32(波罗的海)

我的数据是用 HTML 标记设置的,我一直在尝试使用 display() 函数对其进行修改。我的例子(有点明显)修改了整个单元格。我希望可以使用{{moustaches}} 修改pattern 参数以使嵌套格式成为可能,但我没有任何运气。

library(flextable)
library(officer)
library(dplyr)

data <- structure(list(Description = c("Brill (<em>Scophthalmus rhombus</em>) in subdivisions 22-32 (Baltic Sea)", 
                                       "Cod (<em>Gadus morhua</em>) in subdivisions 22-24, western Baltic stock (western Baltic Sea)", 
                                       "Cod (<em>Gadus morhua</em>) in subdivisions 25-32, eastern Baltic stock (eastern Baltic Sea)", 
                                       "Dab (<em>Limanda limanda</em>) in subdivisions 22-32 (Baltic Sea)", 
                                       "Flounder (<em>Platichthys flesus</em>) in subdivisions 22 and 23 (Belt Seas and the Sound)", 
                                       "Flounder (<em>Platichthys flesus</em>) in subdivisions 24 and 25 (west of Bornholm and southwestern central Baltic)"),
                       SpeciesScientificName = c("Scophthalmus rhombus", "Gadus morhua", "Gadus morhua", "Limanda limanda", "Platichthys flesus", "Platichthys flesus")),
                  .Names = c("Description", "SpeciesScientificName"), 
                  row.names = c(NA, 6L), class = "data.frame")

data %>% 
  mutate(Description = gsub("<em>.*?</em>", "%s", Description)) %>%
  flextable() %>% 
  display(col_key = "Description", pattern = "{{sp_italics}}",
          formatters = list(sp_italics ~ sprintf(Description, SpeciesScientificName)),
          fprops = list(sp_italics = fp_text(italic = TRUE)))

【问题讨论】:

    标签: r flextable officer


    【解决方案1】:

    显示函数正在定义显示值。它不允许仅格式化单元格中文本的一部分,它定义了段落内容及其格式。我将从 tidy data.frame 开始。

    data <- structure(list(id = c("Brill", "Cod", "Cod", "Dab", "Flounder", 
    "Flounder"), SpeciesScientificName = c("Scophthalmus rhombus", 
    "Gadus morhua", "Gadus morhua", "Limanda limanda", "Platichthys flesus", 
    "Platichthys flesus"), subdivision = c("22-32", "22-24, western Baltic stock", 
    "25-32, eastern Baltic stock", "22-32", "22 and 23", "24 and 25"
    ), location = c("Baltic Sea", "western Baltic Sea", "eastern Baltic Sea", 
    "Baltic Sea", "Belt Seas and the Sound", "west of Bornholm and southwestern central Baltic"
    )), class = "data.frame", .Names = c("id", "SpeciesScientificName",  "subdivision", "location"), 
    row.names = c(NA, -6L))
    
    > data
            id SpeciesScientificName                 subdivision
    1    Brill  Scophthalmus rhombus                       22-32
    2      Cod          Gadus morhua 22-24, western Baltic stock
    3      Cod          Gadus morhua 25-32, eastern Baltic stock
    4      Dab       Limanda limanda                       22-32
    5 Flounder    Platichthys flesus                   22 and 23
    6 Flounder    Platichthys flesus                   24 and 25
                                              location
    1                                       Baltic Sea
    2                               western Baltic Sea
    3                               eastern Baltic Sea
    4                                       Baltic Sea
    5                          Belt Seas and the Sound
    6 west of Bornholm and southwestern central Baltic
    

    您的示例是在一段中连接 4 个信息,参数 formatters 将需要 4 个参数...

    ft <- data %>% 
      flextable(col_keys = c("dummy_col", "SpeciesScientificName")) %>% 
      display(col_key = "dummy_col", pattern = "{{id_}} ({{sciname_}}) in {{subdivision_}} ({{location_}})",
              formatters = list(id_ ~ id, sciname_ ~ SpeciesScientificName, subdivision_ ~ subdivision, location_ ~ location ),
              fprops = list(sciname_ = fp_text(italic = TRUE), location_ = fp_text(color="red") )) %>% 
      set_header_labels(dummy_col="whatever") %>% 
      theme_booktabs() %>% autofit()
    
    read_docx() %>% body_add_flextable(ft) %>% print("test.docx")
    

    【讨论】:

    • 这太棒了,非常感谢您的快速回答。作为奖励 - 有没有计划在 flextable/officer 中支持 .docx 的超链接?
    【解决方案2】:

    可能有更简单的解决方案。使用 ftExtracolformat_md() 可以让您进行降价格式化(尽管它似乎不允许纯 HTML)

    https://ardata-fr.github.io/flextable-book/extensions.html#ftextra

    library(ftExtra)
    data.frame(x = c("**bold**", "*italic*"),
               y = c("^superscript^", "~subscript~"),
               z = c("***^ft^~Extra~** is*", "*Cool*")) %>% 
    flextable() %>% 
    colformat_md()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多