Adding image to flextable using function

不羁的心 提交于 2019-12-08 17:31:40

If you add the src in a column of the input data.frame, it should work as expected. I can't reproduce everything as I don't have your data and your images.

library(flextable)
library(tibble)
download.file("https://www.r-project.org/logo/Rlogo.png", destfile = "Rlogo.png")
pupil.tbl <- tribble(
  ~col1, ~col2, ~col3,
  "A", "B", "Rlogo.png",
  "C", "D", "Rlogo.png"
) 
pupil.tbl <- as.data.frame(pupil.tbl)

# display only col1 and col2
pupil.ft <- flextable(pupil.tbl, col_keys = c("col1", "col2") )

add_img_to_flextable <- function(ft, i, j){
  display(
    ft, i=i, col_key = j, pattern = "{{att_tbl}}",
    formatters = list(# use col3 even if not displayed
      att_tbl ~ as_image(col3, src = col3, width = 1.29, height = 1)
    )
  )
}

pupil.ft <- add_img_to_flextable(pupil.ft, i = 2, j = "col2")
pupil.ft

Note that I am not satisfied with the display function, I can see its usage is too complex, I might improve that point later.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!