Transferring R results to word prosessors

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Transferring R results to word prosessors

Tom Backer Johnsen
There has been an incredible number of responses in a short time, with a
number of different suggestions.  With hindsight, I must admit I have not
been quite clear, so additional (somewhat lengthy) explanation is needed.

I want to use R in an introductory course on multiple regression (among
other things) starting in two weeks time for students of psychology at my
University.  These students are very much used to MS Word, it is in
principle possible to get them to adopt OpenOffice (which I would like to),
but I regard Latex to be out of the question.

One of the things they are drilled on is that they have to produce term
papers etc. based on a template in APA (American Psychological Association)
format.  Among other things, this means that the document must be all text
apart from the graphics.  Therefore any kind of solution involving pictures
of tables rather than the tables / results as text is out.  Same holds for
all kinds of "mixed" output, so combinations of text with PDF
elements.  Besides, the tables in R are not that nice in respect to the
formatting.  Since the content is the main thing anyhow, that does not
matter.  In most cases, the tables have to be tweaked as least to some
extent.  Given my inexperience, it seems that the R2HTML path is so far the
most promising (but for me untried so far)

One of the nice things about SPSS and Statistica is that it is VERY easy to
copy and paste output from the program right into the paper / paper.  A
commmon trick when using SPSS is to first paste the output into a
spreadsheet (e.g. Excel), and from there into the document.  In any case,
the outcome is that the output is a table (not a table in the R sense) in
the document, which may be edited, tweaked, adding borders etc..  So, what
I am looking for is a process starting with output from R (like what is
obtained from the summary(lm (...)) command, the output of a correlation
matrix, or ...) that could end up as a table in MS Word (and probably in
OpenOffice as well) in the smallest number of steps.

For instance, if there was an option in R which had the effect that the
spaces separating things (e.g. the columns in the output of a correlation
matrix or the elements in an ANOVA table) were replaced by tabs, everything
would be very simple.  Then, you could (a) paste the output into the
document, and (b) do a simple text-to-table conversion in Word after the
paste.  A simple affair with a few simple steps.  Ideally, what I want for
me and my students is this or a similar solution to this problem.  That
might be a good selling argument for R as well.

Tom

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Frank Harrell
Tom Backer Johnsen wrote:

> There has been an incredible number of responses in a short time, with a
> number of different suggestions.  With hindsight, I must admit I have not
> been quite clear, so additional (somewhat lengthy) explanation is needed.
>
> I want to use R in an introductory course on multiple regression (among
> other things) starting in two weeks time for students of psychology at my
> University.  These students are very much used to MS Word, it is in
> principle possible to get them to adopt OpenOffice (which I would like to),
> but I regard Latex to be out of the question.
>
> One of the things they are drilled on is that they have to produce term
> papers etc. based on a template in APA (American Psychological Association)

There's nothing wrong with the APA template; it will work well with LaTeX.

> format.  Among other things, this means that the document must be all text
> apart from the graphics.  Therefore any kind of solution involving pictures
> of tables rather than the tables / results as text is out.  Same holds for
> all kinds of "mixed" output, so combinations of text with PDF
> elements.  Besides, the tables in R are not that nice in respect to the
> formatting.  Since the content is the main thing anyhow, that does not

You have not seen the various R/latex interfaces for tables then.

> matter.  In most cases, the tables have to be tweaked as least to some
> extent.  Given my inexperience, it seems that the R2HTML path is so far the
> most promising (but for me untried so far)
>
> One of the nice things about SPSS and Statistica is that it is VERY easy to
> copy and paste output from the program right into the paper / paper.  A
> commmon trick when using SPSS is to first paste the output into a
> spreadsheet (e.g. Excel), and from there into the document.  In any case,
> the outcome is that the output is a table (not a table in the R sense) in
> the document, which may be edited, tweaked, adding borders etc..  So, what
> I am looking for is a process starting with output from R (like what is
> obtained from the summary(lm (...)) command, the output of a correlation
> matrix, or ...) that could end up as a table in MS Word (and probably in
> OpenOffice as well) in the smallest number of steps.

It sounds as if you are not interested in teaching students the
principles of reproducible research, which is too bad. [See references
towards the bottom of http://biostat.mc.vanderbilt.edu/StatReport].

>
> For instance, if there was an option in R which had the effect that the
> spaces separating things (e.g. the columns in the output of a correlation
> matrix or the elements in an ANOVA table) were replaced by tabs, everything
> would be very simple.  Then, you could (a) paste the output into the
> document, and (b) do a simple text-to-table conversion in Word after the
> paste.  A simple affair with a few simple steps.  Ideally, what I want for
> me and my students is this or a similar solution to this problem.  That
> might be a good selling argument for R as well.
>
> Tom

--
Frank E Harrell Jr   Professor and Chair           School of Medicine
                      Department of Biostatistics   Vanderbilt University

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Frank Harrell
Department of Biostatistics, Vanderbilt University
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Gabor Grothendieck
In reply to this post by Tom Backer Johnsen
Yes, the R2HTML route is probably the quickest.  Its just one line
of code (plus the call to load in R2HTML).  Try this where iris
is a data set built into R:

   library(R2HTML)
   HTML( iris, file("clipboard","w"), append=FALSE )

Now paste the clipboard into Excel and from there into Word.

(If you are using OO Calc instead of Excel then you need to do:
    Edit | Paste Special | HTML Format | OK
in Calc.)

On 2/9/06, Tom Backer Johnsen <[hidden email]> wrote:

> There has been an incredible number of responses in a short time, with a
> number of different suggestions.  With hindsight, I must admit I have not
> been quite clear, so additional (somewhat lengthy) explanation is needed.
>
> I want to use R in an introductory course on multiple regression (among
> other things) starting in two weeks time for students of psychology at my
> University.  These students are very much used to MS Word, it is in
> principle possible to get them to adopt OpenOffice (which I would like to),
> but I regard Latex to be out of the question.
>
> One of the things they are drilled on is that they have to produce term
> papers etc. based on a template in APA (American Psychological Association)
> format.  Among other things, this means that the document must be all text
> apart from the graphics.  Therefore any kind of solution involving pictures
> of tables rather than the tables / results as text is out.  Same holds for
> all kinds of "mixed" output, so combinations of text with PDF
> elements.  Besides, the tables in R are not that nice in respect to the
> formatting.  Since the content is the main thing anyhow, that does not
> matter.  In most cases, the tables have to be tweaked as least to some
> extent.  Given my inexperience, it seems that the R2HTML path is so far the
> most promising (but for me untried so far)
>
> One of the nice things about SPSS and Statistica is that it is VERY easy to
> copy and paste output from the program right into the paper / paper.  A
> commmon trick when using SPSS is to first paste the output into a
> spreadsheet (e.g. Excel), and from there into the document.  In any case,
> the outcome is that the output is a table (not a table in the R sense) in
> the document, which may be edited, tweaked, adding borders etc..  So, what
> I am looking for is a process starting with output from R (like what is
> obtained from the summary(lm (...)) command, the output of a correlation
> matrix, or ...) that could end up as a table in MS Word (and probably in
> OpenOffice as well) in the smallest number of steps.
>
> For instance, if there was an option in R which had the effect that the
> spaces separating things (e.g. the columns in the output of a correlation
> matrix or the elements in an ANOVA table) were replaced by tabs, everything
> would be very simple.  Then, you could (a) paste the output into the
> document, and (b) do a simple text-to-table conversion in Word after the
> paste.  A simple affair with a few simple steps.  Ideally, what I want for
> me and my students is this or a similar solution to this problem.  That
> might be a good selling argument for R as well.
>
> Tom
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Doran, Harold
In reply to this post by Tom Backer Johnsen
I didn't follow this thread entirely, but I did make a LaTeX recommendation and I know that wasn't what you were asking for. But, if I may, let me respond to the ideas you present below in an attempt to be somewhat persuasive.

IMHO, this are horrible inefficiencies of SPSS and other packages, not virtues. To do what you are suggesting requires that one work in two environments, word and SPSS. If the researcher changes their analysis or wants to tweak the data, then you rerun the analysis, go back to SPSS copy and paste again. Why would someone want to do this when a much more efficient method exists?

Instead, with Sweave, you embed your R code inside the LaTeX document and work in a *single* environment. There is no need to copy and paste and if the data or analysis changes, you update your document very easily saving time, effort, and room for errors. In addition, the tables look much better than word, which (again IMHO) is an aggregiously bad program to begin with.

Using this method, you can place any R code in the document, including graphics, tables (say with xtable) or anything. There is never a need to copy and paste as there is a wonderful, seemless effort between the two programs. Because LaTeX has options for presentations, one can easily create slides that look much better than ppt using a similar method saving hours of effort in my experience.

So, instead of getting R to do what other less sophisticated programs do, which is an effort backwards into the old, and inefficient, ways of doing things, R is moving progressively forward and offers these similar capabilities, but in a much more efficient manner.

Last, if your students are doing technical work, I would suggest they should be familiar with TeX anyhow. It is free, easy to use and learn, offers significant advances alongside R, and equations actually look like equations. BTW, creating and numbering equations in word is about the most difficult effort on earth!

Just my two cents,
Harold  

One of the nice things about SPSS and Statistica is that it is VERY easy to
copy and paste output from the program right into the paper / paper.  A
commmon trick when using SPSS is to first paste the output into a
spreadsheet (e.g. Excel), and from there into the document.  In any case,
the outcome is that the output is a table (not a table in the R sense) in
the document, which may be edited, tweaked, adding borders etc..  So, what
I am looking for is a process starting with output from R (like what is
obtained from the summary(lm (...)) command, the output of a correlation
matrix, or ...) that could end up as a table in MS Word (and probably in
OpenOffice as well) in the smallest number of steps.

For instance, if there was an option in R which had the effect that the
spaces separating things (e.g. the columns in the output of a correlation
matrix or the elements in an ANOVA table) were replaced by tabs, everything
would be very simple.  Then, you could (a) paste the output into the
document, and (b) do a simple text-to-table conversion in Word after the
paste.  A simple affair with a few simple steps.  Ideally, what I want for
me and my students is this or a similar solution to this problem.  That
might be a good selling argument for R as well.

Tom

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Peter Dalgaard
In reply to this post by Gabor Grothendieck
Gabor Grothendieck <[hidden email]> writes:

> Yes, the R2HTML route is probably the quickest.  Its just one line
> of code (plus the call to load in R2HTML).  Try this where iris
> is a data set built into R:
>
>    library(R2HTML)
>    HTML( iris, file("clipboard","w"), append=FALSE )
>
> Now paste the clipboard into Excel and from there into Word.
>
> (If you are using OO Calc instead of Excel then you need to do:
>     Edit | Paste Special | HTML Format | OK
> in Calc.)

Er, if you need to go via Excel anyway, wouldn't it be easier to just
use write.table or write.csv/write.csv2? The real value in R2HTML
would seem to be if you could go directly to Word/OO Writer.


--
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])                  FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Gabor Grothendieck
On 10 Feb 2006 12:39:43 +0100, Peter Dalgaard <[hidden email]> wrote:

> Gabor Grothendieck <[hidden email]> writes:
>
> > Yes, the R2HTML route is probably the quickest.  Its just one line
> > of code (plus the call to load in R2HTML).  Try this where iris
> > is a data set built into R:
> >
> >    library(R2HTML)
> >    HTML( iris, file("clipboard","w"), append=FALSE )
> >
> > Now paste the clipboard into Excel and from there into Word.
> >
> > (If you are using OO Calc instead of Excel then you need to do:
> >     Edit | Paste Special | HTML Format | OK
> > in Calc.)
>
> Er, if you need to go via Excel anyway, wouldn't it be easier to just
> use write.table or write.csv/write.csv2? The real value in R2HTML
> would seem to be if you could go directly to Word/OO Writer.

The data frame was just an example.  The real problem stated by
the user included transferring summary.lm and presumably other
classes of object as well. The HTML solution also in those cases
as it has 158 HTML methods including a summary.lm method.

Another poster did point out already one can create a file from R2HTML
and insert that into Word thereby eliminating the Excel step; however,
I think its actually faster to go the Excel route since that way you don't have
to locate the file in Word but I might be wrong about that so try both and
use whichever you prefer.

On the other hand, the file route has the advantage that if you linked
to it (I have not actually tried this but maybe someone wants to figure
out how this works) rather than insert it (Word supports both linking
and embedding) then if the file changed your Word document automatically
would change too which would address the reproducibility comment that
someone on this thread had.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Gregory Snow
In reply to this post by Tom Backer Johnsen

Just to add a couple of thoughts to the previous suggestions.

If you really want output from things like summary.lm to have tabs instead of spaces then you can type:
getAnywhere(print.summary.lm)

at the R prompt and it will show you the (not quite) source of code that R uses to do the printing to the console.  Copy this to your favorite text editor and use it to create a new function (tabbed.summary.lm?), go through and find the places where it prints output and change the spaces to tabs (in some cases the inserted space is a default and you will not see it directly).  You could even have the cat functions send the output directly to the clipboard rather than the screen.

I had one thought about using capture.output and gsub to replace series of spaces with tabs, but I think that would insert way to many tabs (but I have seen tabify functions elsware (emacs), perhaps that code could be rewritten in R to do what you want).


Others have suggested R2HTML and sweave, but I have not seen anyone else mention that R2HTML has an HTML driver for sweave.  This means that you can write a fairly minimal HTML file (you could even use MSword to set this up, but use a text editor for the later parts) and insert your R commands into the HTML file.  Run this through sweave using the HTML driver and the result is an HTML file (plus extra files for the graphics if any).  MSword can then read in the HTML file and either save it as a word doc or cut and paste from it into a word doc, or best of all, just put everything you need in the original template file and have this be your final product.

R2HTML can also capture an entire set of output into an HTML file (see HTMLStart and HTMLStop) that could then be read into MSword.

Hope this helps,






______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Tom Backer Johnsen
In reply to this post by Tom Backer Johnsen
At 09:25 10.02.2006 -0700, Gregory Snow wrote:

>Just to add a couple of thoughts to the previous suggestions.
>
>If you really want output from things like summary.lm to have tabs instead
>of spaces then you can type:
>getAnywhere(print.summary.lm)
>
>at the R prompt and it will show you the (not quite) source of code that R
>uses to do the printing to the console.  Copy this to your favorite text
>editor and use it to create a new function (tabbed.summary.lm?), go
>through and find the places where it prints output and change the spaces
>to tabs (in some cases the inserted space is a default and you will not
>see it directly).  You could even have the cat functions send the output
>directly to the clipboard rather than the screen.

Interesting alternative.  However, there are at least two drawbacks for me
at least.  First, one would have to make special versions of all the
functions and types of output I might need.  That would easily become a
major job, and probably not worth the trouble.  I will not need to do this
operation often.  Second, I would have to distrubute the procedures to my
students which I would like to avoid.

Tom


+----------------------------------------------------------------+
| Tom Backer Johnsen, Psychometrics Unit,  Faculty of Psychology |
| University of Bergen, Christies gt. 12, N-5015 Bergen,  NORWAY |
| Tel : +47-5558-9185                        Fax : +47-5558-9879 |
| Email : [hidden email]    URL : http://www.galton.uib.no/ |
+----------------------------------------------------------------+

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Tom Backer Johnsen
In reply to this post by Doran, Harold
At 06:09 10.02.2006 -0500, Doran, Harold wrote:

>I didn't follow this thread entirely, but I did make a LaTeX
>recommendation and I know that wasn't what you were asking for. But, if I
>may, let me respond to the ideas you present below in an attempt to be
>somewhat persuasive.

No, you are correct, I was not looking for a LaTex solution.  I would never
want to try to wean my students from an office type package.  That would
not be worth the trouble -- for me.  They should find out about these
things by themseves.  For my own part, I am curious, and will probably have
a look at it.

>IMHO, this are horrible inefficiencies of SPSS and other packages, not
>virtues. To do what you are suggesting requires that one work in two
>environments, word and SPSS. If the researcher changes their analysis or
>wants to tweak the data, then you rerun the analysis, go back to SPSS copy
>and paste again. Why would someone want to do this when a much more
>efficient method exists?

Hmm.  I would think that most users handle a number of applications for
different purposes.  I do not expect R to handle my e-mail, nor do I expect
a spreadsheet to do text formatting.  In itself, that is not a good
argument IMHO.  As to the trouble with cut and paste, well, I do not do
that operation that often.  But I do want things do be simple, simply to
avoid wasting time explaing.

>Instead, with Sweave, you embed your R code inside the LaTeX document and
>work in a *single* environment. There is no need to copy and paste and if
>the data or analysis changes, you update your document very easily saving
>time, effort, and room for errors. In addition, the tables look much
>better than word, which (again IMHO) is an aggregiously bad program to
>begin with.

I have always liked the formatting of the documents I have recognized as
being formatted with LaTex.  And the Sweave concept seems very
interesting.  I have never heard of that system before, so thank you for
mentioning it.  I will have a look at it.

>Using this method, you can place any R code in the document, including
>graphics, tables (say with xtable) or anything. There is never a need to
>copy and paste as there is a wonderful, seemless effort between the two
>programs. Because LaTeX has options for presentations, one can easily
>create slides that look much better than ppt using a similar method saving
>hours of effort in my experience.
>
>So, instead of getting R to do what other less sophisticated programs do,
>which is an effort backwards into the old, and inefficient, ways of doing
>things, R is moving progressively forward and offers these similar
>capabilities, but in a much more efficient manner.
>
>Last, if your students are doing technical work, I would suggest they
>should be familiar with TeX anyhow. It is free, easy to use and learn,
>offers significant advances alongside R, and equations actually look like
>equations. BTW, creating and numbering equations in word is about the most
>difficult effort on earth!

This is students of psychology.  Not technical work, and the number of
formulas per 100 research reports and paper would be closer to 1 per 100
papers than 1 per 10 papers.  They have been using Office type programs
almost since Kindergarten.  That is a simple fact of life (for me at least).

Tom

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Tom Backer Johnsen
In reply to this post by Tom Backer Johnsen
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Tom Backer Johnsen
In reply to this post by Frank Harrell
At 17:10 09.02.2006 -0600, you wrote:

>Tom Backer Johnsen wrote:
>>There has been an incredible number of responses in a short time, with a
>>number of different suggestions.  With hindsight, I must admit I have not
>>been quite clear, so additional (somewhat lengthy) explanation is needed.
>>I want to use R in an introductory course on multiple regression (among
>>other things) starting in two weeks time for students of psychology at my
>>University.  These students are very much used to MS Word, it is in
>>principle possible to get them to adopt OpenOffice (which I would like
>>to), but I regard Latex to be out of the question.
>>One of the things they are drilled on is that they have to produce term
>>papers etc. based on a template in APA (American Psychological Association)
>
>There's nothing wrong with the APA template; it will work well with LaTeX.

I am sure you are right.  But I do not think it is worth the trouble,
neither for me nor the students in respect to this course, to force them
into learn LaTex.  They have been using this type of package almost since
kindergarten (well, not quite).  For me, it is a MUCH simple solution to
show them how the transfer can be done to with simple copy and past
operations.  They will not be doing that operation very often anyhow.

>>format.  Among other things, this means that the document must be all
>>text apart from the graphics.  Therefore any kind of solution involving
>>pictures of tables rather than the tables / results as text is out.  Same
>>holds for all kinds of "mixed" output, so combinations of text with PDF
>>elements.  Besides, the tables in R are not that nice in respect to the
>>formatting.  Since the content is the main thing anyhow, that does not
>
>You have not seen the various R/latex interfaces for tables then.

Correct.  But I intend to have a look in any case.  Sweave has been
mentioned, which do you recommend?  Are there others?

>>matter.  In most cases, the tables have to be tweaked as least to some
>>extent.  Given my inexperience, it seems that the R2HTML path is so far
>>the most promising (but for me untried so far)
>>One of the nice things about SPSS and Statistica is that it is VERY easy
>>to copy and paste output from the program right into the paper /
>>paper.  A commmon trick when using SPSS is to first paste the output into
>>a spreadsheet (e.g. Excel), and from there into the document.  In any
>>case, the outcome is that the output is a table (not a table in the R
>>sense) in the document, which may be edited, tweaked, adding borders
>>etc..  So, what I am looking for is a process starting with output from R
>>(like what is obtained from the summary(lm (...)) command, the output of
>>a correlation matrix, or ...) that could end up as a table in MS Word
>>(and probably in OpenOffice as well) in the smallest number of steps.
>
>It sounds as if you are not interested in teaching students the principles
>of reproducible research, which is too bad. [See references towards the
>bottom of http://biostat.mc.vanderbilt.edu/StatReport].

You are jumping at conclusions.  There is after all a difference between
"not interesting in" and "not knowing about".  That pointer was very nice
and the page had lots of useful information.  Thank you!

>>For instance, if there was an option in R which had the effect that the
>>spaces separating things (e.g. the columns in the output of a correlation
>>matrix or the elements in an ANOVA table) were replaced by tabs,
>>everything would be very simple.  Then, you could (a) paste the output
>>into the document, and (b) do a simple text-to-table conversion in Word
>>after the paste.  A simple affair with a few simple steps.  Ideally, what
>>I want for me and my students is this or a similar solution to this
>>problem.  That might be a good selling argument for R as well.
>>Tom
>
>--
>Frank E Harrell Jr   Professor and Chair           School of Medicine
>                      Department of Biostatistics   Vanderbilt University

Tom

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Gabor Grothendieck
In reply to this post by Tom Backer Johnsen
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Gabor Grothendieck
In reply to this post by Tom Backer Johnsen
On 2/10/06, Tom Backer Johnsen <[hidden email]> wrote:

> At 17:10 09.02.2006 -0600, you wrote:
> >Tom Backer Johnsen wrote:
> >>There has been an incredible number of responses in a short time, with a
> >>number of different suggestions.  With hindsight, I must admit I have not
> >>been quite clear, so additional (somewhat lengthy) explanation is needed.
> >>I want to use R in an introductory course on multiple regression (among
> >>other things) starting in two weeks time for students of psychology at my
> >>University.  These students are very much used to MS Word, it is in
> >>principle possible to get them to adopt OpenOffice (which I would like
> >>to), but I regard Latex to be out of the question.
> >>One of the things they are drilled on is that they have to produce term
> >>papers etc. based on a template in APA (American Psychological Association)
> >
> >There's nothing wrong with the APA template; it will work well with LaTeX.
>
> I am sure you are right.  But I do not think it is worth the trouble,
> neither for me nor the students in respect to this course, to force them
> into learn LaTex.  They have been using this type of package almost since
> kindergarten (well, not quite).  For me, it is a MUCH simple solution to
> show them how the transfer can be done to with simple copy and past
> operations.  They will not be doing that operation very often anyhow.
>
> >>format.  Among other things, this means that the document must be all
> >>text apart from the graphics.  Therefore any kind of solution involving
> >>pictures of tables rather than the tables / results as text is out.  Same
> >>holds for all kinds of "mixed" output, so combinations of text with PDF
> >>elements.  Besides, the tables in R are not that nice in respect to the
> >>formatting.  Since the content is the main thing anyhow, that does not
> >
> >You have not seen the various R/latex interfaces for tables then.
>
> Correct.  But I intend to have a look in any case.  Sweave has been
> mentioned, which do you recommend?  Are there others?
>
> >>matter.  In most cases, the tables have to be tweaked as least to some
> >>extent.  Given my inexperience, it seems that the R2HTML path is so far
> >>the most promising (but for me untried so far)
> >>One of the nice things about SPSS and Statistica is that it is VERY easy
> >>to copy and paste output from the program right into the paper /
> >>paper.  A commmon trick when using SPSS is to first paste the output into
> >>a spreadsheet (e.g. Excel), and from there into the document.  In any
> >>case, the outcome is that the output is a table (not a table in the R
> >>sense) in the document, which may be edited, tweaked, adding borders
> >>etc..  So, what I am looking for is a process starting with output from R
> >>(like what is obtained from the summary(lm (...)) command, the output of
> >>a correlation matrix, or ...) that could end up as a table in MS Word
> >>(and probably in OpenOffice as well) in the smallest number of steps.
> >
> >It sounds as if you are not interested in teaching students the principles
> >of reproducible research, which is too bad. [See references towards the
> >bottom of http://biostat.mc.vanderbilt.edu/StatReport].
>
> You are jumping at conclusions.  There is after all a difference between
> "not interesting in" and "not knowing about".  That pointer was very nice
> and the page had lots of useful information.  Thank you!
>

Also I don't think that that approach is limited to LaTeX.  As I
mentioned in this thread, Word supports linking and embedding of objects
so that when the object changes the Word document dynamically
changes as well.  This may actually be superior to what LaTeX can do
in the sense that with LaTeX you have to regenerate the document
after you have created the LaTeX output but with linking and embedding
you don't -- its dynamic.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: Transferring R results to word prosessors

Philippe Grosjean
In reply to this post by Gabor Grothendieck
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 <[hidden email]> 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
>>
>>______________________________________________
>>[hidden email] mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>>
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html