使用功能将图像添加到flextable [英] Adding image to flextable using function
问题描述
我正在使用Officer创建一个大表的Word文档.我要在此表中插入一些图像.为此,我正在使用flextable.以下代码将图片插入弹性表.
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 :
data.frame中的错误(image_src = src,width =宽度,height =高度,stringsAsFactors = FALSE): 找不到对象"image_file"
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)
)
)
)
}
推荐答案
如果将src添加到输入data.frame的列中,则它应该可以正常工作.我无法复制所有内容,因为我没有您的数据和图像.
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
请注意,我对display
函数不满意,我发现它的用法太复杂了,以后我可能会改进这一点.
Note that I am not satisfied with the display
function, I can see its usage is too complex, I might improve that point later.
这篇关于使用功能将图像添加到flextable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!