从导入的文本文件中着色一个单词,并保存为格式化的单词文档

问题描述 投票:0回答:1

我正在尝试使用R中的官员包为导入的word文档中的一组单词着色,例如word1和word2。在设置单词的文本属性后,我现在卡住了。

我已经使用read_docx(filename)读取了数据文件,其中filename是一个txt文件。我还使用fp_text()ftext()设置文本属性。

my_doc <- read_docx(file.path(filepath,myfile)) 

color1 <- fp_text(color = "blue", font.size = 10, bold = FALSE,
        italic = FALSE, underlined = FALSE, font.family = "Arial",
        vertical.align = "baseline", shading.color = "transparent")
ftext("word1", color1)
color2 <- fp_text(color = "red", font.size = 10, bold = FALSE,
        italic = FALSE, underlined = FALSE, font.family = "Arial",
        vertical.align = "baseline", shading.color = "transparent")
ftext("word2", color1)

但是,如果我的文本类似于This is my text. word1 should be colored blue, and word2 should be colored red,如何将文本中的单词设置为颜色,如何将其保存到应用了颜色的格式化文档中?

r officer
1个回答
0
投票

你是说这个吗?

color1 <- fp_text(color = "blue", font.size = 10, bold = FALSE,
        italic = FALSE, underlined = FALSE, font.family = "Arial",
        vertical.align = "baseline", shading.color = "transparent")
ftext("word1", color1)
color2 <- fp_text(color = "red", font.size = 10, bold = FALSE,
        italic = FALSE, underlined = FALSE, font.family = "Arial",
        vertical.align = "baseline", shading.color = "transparent")
ftext("word2", color2)


fpar_1 <- fpar(ftext("word1", color1), "?")
fpar_2 <- fpar(ftext("word2", color2), "?")

my_doc <- read_docx() %>%
body_add_fpar(fpar_1) %>%
body_add_fpar(fpar_2)

© www.soinside.com 2019 - 2024. All rights reserved.