简体   繁体   English

将图像或pdf插入R中的word文档

[英]Inserting an image or pdf into a word document in R

Im working with a loop that creates many tables etc. and exports it into word documents with ReporteRs package. 我使用循环创建许多表等,并使用ReporteRs包将其导出到word文档中。 So for example I then have a word document with many pages of different graphs, tables and text. 因此,例如,我有一个word文档,其中包含许多不同图形,表格和文本的页面。

I want to insert an image (or pdf - either is fine) into it through the loop (since the loop produces many different word documents). 我想通过循环插入一个图像(或pdf - 要么很好)(因为循环产生许多不同的word文档)。 I have downloaded the ImageMagick and magick packages to work with the images. 我已经下载了ImageMagick和magick包来处理图像。 Now I have my image in R, but I cant figure out how to add it to my document. 现在我在R中有我的图像,但是我无法弄清楚如何将它添加到我的文档中。

I know that ReporteRs has an addImage command that inserts external images (honestly im having trouble figuring that one out to). 我知道ReporteRs有一个插入外部图像的addImage命令(老实说我很难搞清楚那个)。 Is it possible adding internal images/pdf's to a document? 是否可以在文档中添加内部图像/ pdf?

Hope you guys can gives me some tips. 希望你们能给我一些提示。 Thank you in advance! 先感谢您!

I strongly recommand to migrate your code to officer as ReporteRs will be removed from CRAN on 2018-07-16. 我强烈建议将您的代码迁移到officer因为ReporteRs将于ReporteRs从CRAN中删除。 From the code @d125q wrote, this would be transformed as : 从代码@ d125q写的,这将被转换为:

library(officer)
library(magick)

download.file("https://jeroen.github.io/images/frink.png", "frink.png")
dims1 <- attributes(png::readPNG("frink.png"))$dim/72
sample.image <- image_read("frink.png")
image_write(image_rotate(sample.image, 45), "frink_rotated.png")
dims2 <- attributes(png::readPNG("frink_rotated.png"))$dim/72


sample.doc <- read_docx()
sample.doc <- body_add_img(sample.doc, src = "frink.png", width = dims1[2], height = dims1[1] )
sample.doc <- body_add_img(sample.doc, src = "frink_rotated.png", width = dims2[2], height = dims2[1] )
print(sample.doc, target = "sample.docx")

You can plot images from magick to add them to a document using ReporteRs . 您可以使用ReporteRs plot来自magick图像以将其添加到文档中。 Here's an example: 这是一个例子:

library(ReporteRs)
library(magick)

sample.doc <- docx(title="Sample")

## add original Frink
sample.image <- image_read("https://jeroen.github.io/images/frink.png")
sample.doc <- addPlot(sample.doc,
                      fun=plot,
                      x=sample.image)

## add rotated Frink
sample.doc <- addPlot(sample.doc,
                      fun=function(x) plot(image_rotate(x, 45)),
                      x=sample.image)


## save the document to disk
writeDoc(sample.doc, "sample.docx")

If anyone is wondering about this with the new officer. 如果有人对这位新军官感到疑惑。 I needed to insert a pdf into my document. 我需要在我的文档中插入pdf。 I converted the pdf to a picture. 我将pdf转换为图片。 After migrating to officer, i ended up simply using this code from the officer package: 迁移到官员后,我最终只是使用官方包中的代码:

img.file <- file.path( R.home("doc201"), "P:/path to my picture", "name.png" )

doc201 <- body_add_img(x = doc201, src = "P:/path/name.png", height = 10, width = 6, pos = "after" )

The other answers worked too, but after i got used to officer this was the most simple way for me. 其他答案也有效,但在我习惯了官员后,这对我来说是最简单的方法。 Hope this is of help in the future! 希望这对未来有所帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM