Adding image to flextable using function

2019-06-08 07:55发布

问题:

I am using Officer to create a Word document, which is a large table. Into this table I want to insert some images. To do this I am using flextable. The following code inserts an image into the flextable.

pupil.tbl <- tribble(
  ~col1, ~col2,
  paste("Name:", pupil$name), paste("Class:", pupil.class),
  "attendance_graph", "boxall_graph"
) 
# add attendance plot
pupil.ft <- flextable(as.data.frame(pupil.tbl))
pupil.ft <- display(
  pupil.ft, i=2, col_key = "col1", pattern = "{{att_tbl}}",
  formatters = list(
    att_tbl ~ as_image(
                 col1, 
                 src = "attendance.png", 
                 width = 3.3, 
                 height = 1.65)
               )
  )
)

This works fine, but I have quite a few images to add so I thought I would abstract it into a function. However when I try to do this I get :

Error in data.frame(image_src = src, width = width, height = height, stringsAsFactors = FALSE) : object 'image_file' not found

Here is the function and a call to the function(at the moment it is using the global variables for everything except the path to the image)

pupil.ft <- add_img_to_flextable("attendance.png")

add_img_to_flextable <- function(image_file){
  return(
    display(
      pupil.ft, i=2, col_key = "col2", pattern = "{{att_tbl}}",
      formatters = list(
        att_tbl ~ as_image(
                    col1, 
                    src = image_file, 
                    width = 3.3,  
                    height = 1.65)
      )
    )
  )
}

回答1:

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.