[R] Transferring R results to word prosessors

Philippe Grosjean phgrosjean at sciviews.org
Sat Feb 11 10:40:01 CET 2006


The copy to clipboard feature is written (by Eric Lecoutre & myself) 
since a couple of years. It is in the SciViews bundle, library svIO. In 
this library, you have:
- copy() that copies an object to the clipboard in various formats 
(icluding HTML, by using R2HTML and LaTeX),

- export() does the same, but save to a file on disk,

- you have also clippaste() to paste data from the clipboard.

Moreover, in the SciViews bundle, you have also svViews that provides 
further interesting functions for reporting:
- view() allows to generate different rich-formatted views on objects,

- report() sends a view to a reporting application (but see hereunder 
the note about SciViews-R),

- reportGraph() is the same for R graphs,

... All these functions exist on CRAN since at least two years. They are 
programmed under Windows(), but most of them should be working on other 
platforms (I would gladly consider patches, of course, if required).

Now, if you want a convenient point and click approach to write a 
report, you should use SciViews-R (http://www.sciviews.org/SciViews-R). 
It has a nice GUI interface on top of these functions. For instance, you 
can access them from the context menu of the object explorer (but see 
the SciViews-R manual at http://www.sciviews.org/SciViews-R/Manual.pdf).

SciViews-R is Windows only, but I am gradually moving code to a 
platform-independent solution (lot of code, lack of time, other 
priorities,... so, you have to be patient!).

Finally, I uploaded the latest version of SciViews this week on CRAN. 
Now, the svViews package has a suite of WordXXX() functions. There are 
intended for sending nicely formatted data to Word. The next version of 
SciViews-R (next week, or so) included the functionnalities in the GUI.

Regarding reproducibility of results, the strategy here is to write a 
template in Word with bookmarks at the locations where you want to put 
material coming from R. Once it is done, you can control entirely the 
process of filling the Word document from within a R script. For 
instance (extracted and reworked from the example in ?WordOpen):


# Install SciViews bundle from CRAN, then...
library(svViews)
repdir <- "c:/temp" # Adapt this for you
WordOpen(file.path(repdir, "report.doc")) # Open your template

# Code to create a graph in a file, for instance
graphfile <- file.path(repdir, "figure1.emf")
win.metafile(filename = graphfile, width = 7, height = 5)
hist(rnorm(500))
dev.off()

# Insert that graph in Word at a location named "picture1":
WordGoto("figure1") # Move to the picture1 bookmark
WordInsertPictureFile(graphfile, TRUE) # Insert the graph

# Create a view in HTML (rich-formatted) and insert it in Word
# for instance, for a simple linear regression, you can do:
data(trees)
trees.lm <- lm(Volume ~ Girth, data = trees)
viewfile <- view(trees.lm, type = "summary", browse = FALSE)
WordGoto("view1") # Go to the regression1 bookmark in your report
WordInsertFile(viewfile, TRUE) # Insert the view in the report

# This is done!
WordActivate(async = TRUE) # Show your report in Word.
# and allow to refine it (add comments according to the results)

# -- or --
# WordExit()  # Close word, possibly prompting to save the doc.


(note that I appended a suitable "myreport.doc" to this email, but it 
will probably be eliminated on the mailing list. so, ask me if you need it).
Best,

Philippe Grosjean

P.S.: the formatting of text and tables in the views is managed through 
CSS files, independently from Word styles. So, you would presumably like 
to make a CSS file in accordance with the style you want in your report.


Gabor Grothendieck wrote:
> Given that this may very well be the most common use of the
> R2HTML package I wonder if the R2HTML package developer would
> be interested in providing an HTML2clip convenience wrapper
> as part of the R2HTML package like this:
> 
>    HTML2clip <- function(x, file. = file("clipboard", "w"), append = FALSE, ...)
>      HTML(x, file = file., append = append, ...)
> 
> so that one could just write:
> 
>    HTML2clip(summary(lm(rating ~., attitude)))
> 
> On 2/10/06, Tom Backer Johnsen <backer at psych.uib.no> wrote:
> 
>>Thank you all for very useful and interesting responses.  After reading the
>>comments and after some experiments, I added the following to a text I will
>>be handing out to the students (I would not mind comments):
>>
>><QUOTE>The contents of the text output from R may be very sophisticated,
>>but the formatting of the texts is always very simple, with absolutely no
>>frills.  For instance, all formatting of columns is managed with spaces or
>>blanks, no tabs, nothing extra apart from line feeds.  This means that
>>transferring some types of output, like the summary of the multiple
>>regression in part 7.4 directly to MS Word or any other wordprocessor would
>>be far from optimal.  To make a decent table for presenting results in a
>>paper in APA format, we need a "table" in the word processing sense, an
>>arrangement of things in rows and columns.  With output as plain as in R, a
>>lot of fiddling would be necessary after a direct copy and paste of the
>>text into MS Word.  So, we need a better solution.
>>
>>The steps involved are really quite simple, the information is transferred
>>via a speadsheet:
>>
>>1. Write the output to the clipboard in HTML format (that is the same
>>format as used for writing web pages)
>>
>>2. When you are finished with that, paste the contents of the clipboard
>>into a spreadsheet (e.g. Excel).  This automatically reformats HTML to
>>something that both the spreadsheet and the word processor (e.g. MS Word)
>>can handle.
>>
>>3. Copy and paste what you need from the spreadsheet to the document.
>>
>>The last two steps are the same as when using Statistica or SPSS in a
>>anyhow.  Especially SPSS has a tendency include too much formatting when
>>pasting, and then Excel is a useful stepping stone to strip off the frills.
>>
>>The main difference is in the first step.  What we need there is to write
>>the output from R to the clipboard in a format that Excel recognizes as
>>something with columns and rows.  For an example, consider the "summary ()"
>>output from the multiple regression in part 7.4 above.
>>
>>First, you have to make the library "R2HTML" available to the session:
>>
>> > library (R2HTML)
>>
>>You only need to do this once in a session.  If this package is not
>>installed, have a look at part 9 above.  Then we need to attach the data
>>set and do the multiple regression:
>>
>> > attach (attitude)
>> > Results <- lm (rating ~ complaints + privileges + learning)
>> > HTML (summary (Results, digits=4), file("clipboard", "w"), append=FALSE)
>> > detach (attitude)
>>
>>If you do this more than a few times, it might be a good idea to write a
>>function as a replacement for the HTML command with a reasonable name, e.g.
>>"ToClip".  The last command could then be replaced by:
>>
>> > ToClip (summary (Results, digits=4))
>>
>>Which is much simpler.  In any case, the results are now writtten to the
>>clipboard.  Open Excel, and paste the contents into a worksheet.  Select
>>what you want, copy it to the clipboard, and then open your document where
>>the paper is found.  Locate the place where you want the table, and paste
>>the clipboard there. </QUOTE>
>>
>>After showing how the table looks, I mention that some details will have to
>>be fixed, like conversion of the lower part of the output to text, removing
>>blank rows, adding borders etc., plus rewriting the p value which is in
>>scientific notation.  It is still not in an APA format, but very much
>>better than it would bave been with a simple "copy and past" operation.
>>
>>Tom
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> 


More information about the R-help mailing list