The last post published in FreshBiostats, by Hèctor Perpiñán, was about reports combining the use of R and Latex. In it, he explained how to transport tables and figures from the R output to Latex.
Well then, this week I shall continue talking about another tool to write reports when we do the statistical analysis with R. It allows us to reduce the time spent to draft the reports and be more accurate by minimising human copying errors. I am going to focus my attention on “R2wd” package to write MS-Word documents from R.
Although most statisticians are used to work daily with Latex which results in high quality appearance, in most cases our clients have never heard about it and prefer to work with MS-Word documents, which are easier to handle.
The R2wd package needs either the statconnDCOM server (via the rcom package) or the RDCOMClient to communicate with MS-Word via the COM interface. Once this interface is installed (on Windows OS), we can create a document using all functions available in this package: wdGet, wdTitle, wdSection, wdType, wdSetFont, wdTable (the object must be a data frame or an array), wdPlot, wdApplyTheme, etc.
Not only do these functions allow us to introduce tables or figures from R output, but can also be used to inject text elements into Word report, themes and templates, different type of text (normal, italic, Verbatim,…), etc.
The following code shows a little example on how to use this package:
</pre> ### An easy example to use "R2wd" package ### library("R2wd") #of course, you must install it before! #require("rcom") wdGet() #To open a new Word document #It is possible that we get an error message with wdGet() function. #In this case we will need to install: #installstatconnDCOM() wdTitle("We introduce here our file title") wdSection("First section begins here") wdBody("Inserts text in 'Body' style at the current cursor point in Word.") wdWrite("The function wdBody is similar to 'wdWrite'",paragraph=TRUE) wdType("Now we use an italic type and a centering text",italic=TRUE,alignment="center") wdNormal("To return a Normal type") wdWrite("We can also insert footnotes") wdInsertFootnote("insert a footnote") #you can insert footnotes in the #word document at the cursor position. wdSection("Now we include another section") wdSubsection("A toy example") wdBody("Insert tables of results") age <- c(23,12,45,34,18,41) gender <- c(1,1,0,1,0,0) height <- c(172, 150,169,180,188,160) data <- data.frame(age,gender,height) wdTable(data,autoformat=2)#three possibilities to autoformat parameter wdItemize() wdWrite("insert a text in the bullet",paragraph=TRUE) wdItemize(Template=3) wdWrite("we finish the description",paragraph=TRUE) wdBody("To finish this easy example we include a plot in our Word document") wdPlot(data$age,data$height,col="red",pch=20,height = 10, width =10, pointsize = 25) wdSave() wdQuit() #To close the session
As you have seen, besides being able to format our report as we want (although it may seem cumbersome to do from R), R2wd is really useful because with an only function – wdTable() – and one “click” I can copy a large table in my report with no mistakes.
You will find more functions here. Dare to try it! You will save plenty of time!