I want to modify the format of a portion of a cell, while leaving the remaining text unmodified for inclusion in a .docx report:
e.g., Brill (Scophthalmus rhombus) in subdivisions 22-32 (Baltic Sea)
My data is set up with HTML tags, which I have been trying to modify using the display()
function. My example (somewhat obviously) modifies the entire cell. I expect that the pattern
argument can be modified with {{moustaches}}
to make the nested formatting possible, but I haven't had any luck.
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)))