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)
)
)
)
}