我想修改部分单元格的格式,同时保留未修改的剩余文本以包含在 .docx 报告中:
例如,Brill ( Scophthalmus rhombus ) 在细分 22-32 (Baltic Sea)
我的数据是使用 HTML 标签设置的,我一直在尝试使用该display()
函数对其进行修改。我的例子(有点明显)修改了整个单元格。我希望pattern
可以修改参数{{moustaches}}
以使嵌套格式成为可能,但我没有任何运气。
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)))