【问题标题】:R language: Writing a JPEG into an RTF document using RTF packageR语言:使用RTF包将JPEG写入RTF文档
【发布时间】:2016-06-26 17:23:05
【问题描述】:

我正在使用 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,width = width,: 找不到函数“plot.fun” 完成(rtf)

【问题讨论】:

  • 谢谢,马丁。这真的很有帮助。感谢您的及时回复!

标签: r jpeg rtf


【解决方案1】:

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)

【讨论】:

  • 谢谢,digEmAll。您的详细回复真的很有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 2011-07-11
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 2011-12-30
相关资源
最近更新 更多