我正在使用RTF包,并有一个JPEG文件,我想要添加到RTF文档,我正在创建。但是,我知道我可以使用RTF包的"addPlot()“函数,但我似乎无法使它工作。也许,我需要把JPEG转换成一个“阴谋?”如果是的话,我不知道怎么做。
谢谢你提前提供帮助!
下面是我的代码和错误消息:
output <- "DownloadImage_proof_of_concept.doc"
rtf <- RTF(output,width=8.5,height=11,font.size=10,omi=c(1,1,1,1))
addPlot(rtf, plot.fun="y.jpg", width = 4, height = 5, res=300).rtf.plot中的错误(plot.fun= plot.fun,tmp.file = tmp.file,宽度=宽度,:未能找到函数"plot.fun“done)
发布于 2016-06-26 07:11:53
addPlot需要在plot.fun中传递一个函数,而不是像文档中所描述的那样是jpeg文件名(参见help(addPlot.RTF))。
问题是,您需要先绘制一个jpeg,这不是最简单的事情。
总之,多亏了this answer,我们才能做到这一点:
library(rtf)
# function defined in the linked answer
# Note: you need to install jpeg package first
plot_jpeg = function(path, add=FALSE){
require('jpeg')
jpg = readJPEG(path, native=T) # read the file
res = dim(jpg)[1:2] # get the resolution
if (!add) # initialize an empty plot area if add==FALSE
plot(1,1,xlim=c(1,res[1]),ylim=c(1,res[2]),asp=1,type='n',xaxs='i',
yaxs='i',xaxt='n',yaxt='n',xlab='',ylab='',bty='n')
rasterImage(jpg,1,1,res[1],res[2])
}
output<-"test.rtf"
rtf <- RTF(output,width=8.5,height=11,font.size=10,omi=c(1,1,1,1))
addPlot(rtf, plot.fun=function(){plot_jpeg('myfile.jpg')}, width = 4, height = 5, res=300)
# ...
done(rtf)无论如何,如果您有jpeg映像,我建议简单地在png中转换它,然后使用方便的函数addPNG,例如:
output<-"test2.rtf"
rtf <- RTF(output,width=8.5,height=11,font.size=10,omi=c(1,1,1,1))
addPng.RTF(rtf,file = "myfile.png", width = 4, height = 5)
# ...
done(rtf)https://stackoverflow.com/questions/38035764
复制相似问题