我有一个 zip 文件,其中包含 2 种类型的多个图像。1-FrequencyRose 图像 2-EnergyRose 图像。我创建了一个 flextable,然后使用for loop将偶数行替换为图像,将奇数行替换为图像标题。该循环正确显示标题,但它仅多次打印每种类型的最后读取图像,而不是根据循环计数实际打印所有图像。
img.file <- unzip("D:\\Mast_Image Files.zip")
fr_files <- img.file[grepl(paste("FrequencyRose", collapse = "|"), img.file)]
er_files <- img.file[grepl(paste("EnergyRose", collapse = "|"), img.file)]
fr_files 有 3 个图像文件路径,与 er_files click相同
num_masts = length(img.file)
c1 = rep("Freq_rose",num_masts)
c2 = rep("Energy_Rose",num_masts)
df = data.frame(c1,c2)
dfft = flextable(df)
sso=seq(1,num_masts,2)
sse=seq(2,num_masts,2)
for (g in 1:(num_masts/2)){
ff.img = fr_files[g]
ef.img = er_files[g]
dfft2 = dfft %>%
display(
i = sse[g], col_key = "c1", pattern = "{{img}}",
formatters = list( img ~ as_image(c1,
src = ff.img, width = 3, height = 3))) %>%
display(
i = sse[g], col_key = "c2", pattern = "{{img}}",
formatters = list( img ~ as_image(c2,
src = ef.img, width = 3, height = 3))) %>%
display(
i = sso[g], col_key = "c1", pattern = paste("Freq_Rose","mast",g)) %>%
display(
i = sso[g], col_key = "c2", pattern = paste("Energy Rose","mast",g))
}
此循环能够正确生成标题,但唯一的 fr_files[3]、er_files[3] 正在循环对应列的所有偶数行。输出为:最终结果。找不到问题。