How can one copy-paste local png files to a word document using R?

3k views Asked by At

I have ~10,000 png images saved neatly in different files on my PC. I want to write a function that does something like go to a particular folder and iteratively copy-pastes all the png files in that folder to a word document. Is this possible in R?

I've looked at package R2wd but it sadly only has a function that takes RData and outputs its plot to a word document (function wdPlot).

I also have the RData saved for each and every plot, so reason would dictate that I should be able to simply load the RData associated with a particular plot and then use wdPlot . The problem is that when I generated my png's the plots were grobs and I did something as follows:

png("rp.png",width=w,height=h)
plot(rp)
#Increase size of title
grid.edit(gridTitle_Ref, gp=gpar(fontsize=20))
#Other grid.edit alterations
dev.off()
save(rp)

Now, when I try to get that rp onto a word document by first loading it into R I naively do the following and it does not output a plot to MS Word with the title enlarged or any of the other grid.editalterations.

load("rp.Rdata")    
png("rp.png",width=w,height=h)
wdPlot(rp) 
#Increase size of title 
grid.edit(gridTitle_Ref, gp=gpar(fontsize=20))
#Other grid.edit alterations
dev.off()

So, to reiterate: I have all these png files. At various times I have to copy-paste a subset of them into a word document. I'm too lazy to do that manually each time and want a program to do it for me.

EDIT 1

So, as per suggestions below, I've read up on Markdown. Following this post How to set size for local image using knitr for markdown? I wrote something along the lines of:

```{r,echo=FALSE,fig.width=100, fig.height=100}
# Generate word documents of reports
# Clear all
rm(list=ls())
library(png)
library(grid)
library(knitr)

dir<-"location\of\file"
setwd(dir)

# Output only directories:
folders<-dir()[file.info(dir())$isdir]

for(folder in folders){ 
  currentDir<-paste(dir,folder,"\\",sep="")
  setwd(currentDir)

  #All files in current folder
  files<-list.files()

  imgs<-[A list of all the png images in this particular file that I want in the word document - the png names]

  for(img in imgs){   
    imgRaster<-readPNG(img) 
    grid.raster(imgRaster)
    }
 } 

```

The following is a screenshot of what's in the resulting word document. How might I fix this? I want the images to appear one after the other in the document as the for loop above runs.

enter image description here

Do note that this is the first time I've ever used Markdown so any relevant tutorials linked in the comments could also be of great help.

EDIT 2 I followed the second answer's example below. Here is the output that I obtained enter image description here enter image description here

As you can see there are no images, only the html tags. How do I fix this?

2

There are 2 answers

10
user20650 On BEST ANSWER

If you have the png's saved you can just use a little html and a for loop to save them to a .doc file.

edit 2 for windows

# Start empty word doc
cat("<body>", file="exOut.doc", sep="\n")

 # select all png files in working directory
for(i in list.files(pattern="*.png"))
         {
         temp <- paste('<img src=', i, '>')
         cat(temp, file="exOut.doc", sep="\n", append=TRUE)  
         }

cat("</body>", file="exOut.doc", sep="\n", append=TRUE)

# Some example plots
for(i in 1:5) 
      { 
      png(paste0("ex", i, ".png"))
      plot(1:5)
      title(paste("plot", i))
      dev.off()
      }


# Start empty word doc
cat(file="exOut.doc")

# select all png files in working directory
for(i in list.files(pattern="*.png")) 
             {
             temp <- paste('<img src=', i, '>')
             cat(temp, file="exOut.doc", sep="\n", append=TRUE)   
             }

You will then need to embed the figures, either using the drop down menus or by writing a small macro that you can call with system


EDIT : small update to show explicit paths to output and figures

cat("<body>", file="/home/daff/Desktop/exOut.doc", sep="\n")

for(i in list.files(pattern="*.png")) 
{
  temp <- paste0('<img src=/home/daff/', i, '>')
  cat(temp, file="/home/daff/Desktop/exOut.doc", sep="\n", append=TRUE)   
}

Note that i used paste0 to remove the space between the path /home/daff/ and ex*.png.

3
Robert On

Have you tried Rstudio and Markdown? You could put your code into chunks that load the files and save as word document. http://rmarkdown.rstudio.com/word_document_format.html