R: Can the R2wd package be used to iterativly insert a table at the end of a word file? How?

404 Views Asked by At

I have a file that I open using wdGet(filename="exOut.doc",visible=FALSE). This file already has images in it that I've inserted using html and cat(img, file=outputDoc, sep="\n", append=TRUE).

I need to insert a table at the end of the document, but wdTable(format(head(testTable))) places the table at the very top of the word document. How can I fix this?

Also, second problem: I have a lot of tables I need to insert into my document and hence make use of a loop. Below is sample code that demonstrates my problem. Here's the really weird part for me: when I step through the code and run each line after another, it produces no error and I have an output document. If I run everything at once I get a 'cannot open the connection error'. I don't understand how this can be. How is it possible that running each line one at a time produces a different result than running all of that exact same code all at once?

rm(list=ls())

library(R2wd)
library(png)

outputForNow<-"C:\\Users\\dirkh_000\\Downloads\\"
outputDoc<-paste(outputForNow,"exOut.doc",sep="")
setwd(outputForNow)

# Some example plots
for(i in 1:3) 
{ 
  dir.create(file.path(paste("folder",i,sep="")))
  setwd(paste("folder",i,sep="")) # Note that images are all in different folders
  png(paste0("ex", i, ".png"))
  plot(1:5)
  title(paste("plot", i))
  dev.off()
  setwd(outputForNow)
}

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

# Retrieve a list of all folders
folders<-dir()[file.info(dir())$isdir]
folders<-folders[!is.na(folders)]

# Cycle through all folders in working directory
for(folder in folders){
  setwd(paste(outputForNow,folder,sep=""))
  # select all png files in working directory
  for(i in list.files(pattern="*.png"))
  {
    temp<-paste0('<img src=','\"',gsub("[\\]","/",folder),"/", i, '\">')
    cat(temp, file=outputDoc, sep="\n", append=TRUE)
    setwd(paste(outputForNow,folder,sep=""))
  }
  setwd(outputForNow)
  cat("</body>", file="exOut.doc", sep="\n", append=TRUE)
  testTable<-as.data.frame(cbind(1,2,3))
  wdGet(filename="exOut.doc",visible=FALSE)
  wdTable(format(head(testTable))) ## This produces a table at the top and not the bottom of the document
  wdSave(outputDoc)
  wdQuit() # NOTE that this means that the document is closed and opened over and over again in the loop otherwise cat() will throw an error
}

The above code produces:

Error in file(file, ifelse(append, "a", "w")) : 
  cannot open the connection

Can anyone tell me why this occurs and how to fix it? Please and thank you. Please do recommend a completely different approach if you know I'm going about this the wrong way, but please also explain what it is that I'm doing wrong.

1

There are 1 best solutions below

4
On

To start the DescTools package and a Word document, use something like this (obviously, modified for your path structure):

library(DescTools)
library(RDCOMClient)
report <- GetNewWrd(template = "C:/Users/Rees/Documents/R/win-library/3.0/R2DOCX/templates/TEMPLATE_03.docx")

ADDED BASED ON COMMENT

Create a template for your report in Word. Perhaps you call it TEMPLATE.docx. Save it in your Document director (or whatever directory you keep Word documents in. Then

report <- GetNewWrd(template = " "C:/Users/dirkh_000/Documents/TEMPLATE.docx")

Thereafter, each time you create a plot, add this line:

WrdPlot(wrd = report)

The plot is inserted in the TEMPLATE.docx Word document in the specified directory.

The same for WrdTable(wrd = report)