Re: [R] Transferring R results to word prosessors

From: Tom Backer Johnsen <>
Date: Sat 11 Feb 2006 - 06:45:44 EST

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 mailing list PLEASE do read the posting guide! Received on Sat Feb 11 06:49:51 2006

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:42:27 EST