0

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.

Frikster
  • 2,755
  • 5
  • 37
  • 71
  • Have you compared the R2wd package to the Word capabilities in the DescTools package? Or those in the ReporteRs package? I have used both of them and not had any "open connection" errors. – lawyeR Nov 29 '14 at 01:57
  • I cant import the ReporteRs package. It looks like it needs the ReporteRsjars package. But when I try to load ReporteRsjars it looks like it needs rJava. Then finally when I load rJava I get a .onLoad failed in loadNamespace() error. As for DescTools, how does one save a document!? I can use GetNewWrd and WrdTable to insert a table into a newly created document object, but then the resulting word document isn't saved anywhere. – Frikster Dec 01 '14 at 00:04

1 Answers1

0

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)

lawyeR
  • 7,488
  • 5
  • 33
  • 63
  • How do you know where that template is located? And isn't it sufficient to simply use the default? My problem is that I cant find where the report Word document is saved (i.e. which directory is it saved to?) – Frikster Dec 01 '14 at 00:46
  • Have you called getwd()? That will tell you where the Word document is saved. You can store the template wherever you want and use the path to identify where. Put it in your main Word directory, for example. – lawyeR Dec 01 '14 at 01:05
  • I do the following: report <- GetNewWrd(visible=FALSE) (no errors) and then getwd() which gives "C:/Users/dirkh_000/Downloads". I then navigate to the downloads folder and cant find any word document saved. – Frikster Dec 01 '14 at 01:12
  • Dirk, see my answer and its edit. You have to set up a template document somewhere. You then use GetNewWrd to assign that template, which is a Word document file, to your R object, which I call "report" – lawyeR Dec 01 '14 at 02:15