Using RinWord

J.A.Hartigan  Yale University October 2013

1 RinWord

RinWord produces wysiwyg reports on statistical analysis using R, in which both the word formatted commentary and R commands may be edited.

 RinWord is an R script that operates on an rtf (rich text format) file containing R commands, and other elements of a word file such as pictures and commentary: 

(1) The R commands  ( identified as text in Courier New font) and commentary are converted to an R script,

(2) The script is executed to produce a standard R log file consisting of commands and output, with pictures produced in separate files.

 (3) The log file is then converted to the original rtf format with pictures and the original commentary re-included.

Of course, it would be wrong, wrong,  if this report was not a RinWord product.

2 An Example

Some comments and commands:
x <- 1:10
print(x)

tiff("fool.tif",  w=400., h=400)
plot(rnorm(1000), rnorm(1000), col =1:1000)
dev.off()

Note that the commands are in Courier New.  The comments may be in any font you like, except Courier New!  Of course, Adam and Eve will immediately start writing snappy comments in Courier New.  They will be punished, but not banished.

Copy  "RinWord.R" into the directory where this file is located,  make that directory the working directory for R, and begin with

source("RinWord.R"). 

You only need to do this once at the beginning of the session.
Then 

RinWord("Using RinWord.rtf")
 ( For Myfile,  RinWord("Myfile.rtf") )

This is not recognised as an R command, wrong font!  I will go do these commands in R, and see what happens to this file. (Only  commands between the bright red comment lines will be executed, but all lines will be processed if there are no red comment lines) .  Because Word insists on controlling exits from itself,   you must exit the rtf file  before running RinWord.

3  The result of the first RinWord run

What happened was that output and pictures were inserted, and the rtf file is reopened.  

Some comments and commands:
x <- 1:10
print(x)

[1]  1  2  3  4  5  6  7  8  9 10

tiff("fool.tif",  w=400., h=400)
plot(rnorm(1000), rnorm(1000), col =1:1000)
dev.off()

Note that the commands are in Courier New.  The comments may be in any font you like, except Courier New!  Of course, Adam and Eve will immediately start writing snappy comments in Courier New.  They will be punished, but not banished.

4 Pictures

You need to retain the picture files produced by R for them to appear in the RinWord rtf file.  If you make a pdf file from the final product, you will not need the picture files to support the the pdf file. 

Suppose you have a picture  "ndp041_temp.png"  in your working directory. Then include it with  the R function:

picture( "ndp041_temp.png") 

Can you guess why people in latitudes 40-50 degrees are always whining about the weather?  Whereas Greenlanders, neh?

5 Error messages

Uncheck "Buffered Output" in the Misc menu on the R  windows tool bar. While running see:

rtf to R 
R to log 
log to rtf 

These console messages track the progress of the conversions from rtf to R script, from R script to log, and from log back to the original rtf.    The R to log conversion will fail when  you have bad R code. You have to take care of that. 

 If one of the other two conversions fail,  try redlining bits of questionable code to isolate the problem.  You might try changing (FALSE) to (TRUE) in RinWord to get a world of wordy diagnostics, but unless you know rtf it won't help much.

Let us try an error.

Adam and Eve like this kind of thing.  no problem.
Adam and Eve like this kind of thing.  problem.

R gives the following message:
rtf to R 
Error in source(fpscript, echo = TRUE, max.deparse.length = 1e+05) : 
  f.R:86:6: unexpected symbol
85: ##
86: Adam and

I told you and told you, but no, you had to see for yourself.  The Garden of Eden is supposedly located near Aleppo, so you can stay there for the time being, but keep this up, you can expect a little rain!

There is no change to the RinWord  file  when a failure occurs, and the rtf file is not reopened. You need to find and fix the R error  indicated on the console display.