18

what's the best way to generate a word-processor file (ideally as platform-independent as possible, but native Word formats will do), with images in it, from the statistical language R?

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
John Lawrence Aspden
  • 17,124
  • 11
  • 67
  • 110
  • Thanks to all who answered. I'm currently using odfWeave and it appears to be working a treat for what I want. A particularly nice feature is that it puts R output into an odf template, so non-wizards can reformat the document and move things around as long as they don't touch the code sections. It's also easy to put complicated code into a script, and just use odfWeave as an output formatter. I think it's a perfect solution, and better than I'd hoped to find. – John Lawrence Aspden Nov 11 '10 at 13:44

6 Answers6

13

The standard Sweave engine is one option if you can handle LaTeX - but I guess that is stretching the "word-processor file" aspect just a touch! Alternatively, odfWeave is a related package providing a new engine for Sweave that will work with OpenOffice.org documents.

There are other options on the Reproducible Research Task View on CRAN, although some of the MS Office oriented options require Windows specific cruft.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • 4
    odfWeave is probably what you are looking for. – Choens Nov 10 '10 at 20:39
  • Thanks! odfweave looks like the winner here. – John Lawrence Aspden Nov 10 '10 at 22:24
  • It sure does, but looking into Sweave is really worth it too if you can afford the time. – Matt Bannert Nov 11 '10 at 08:58
  • @ran2 - I agree (if you mean Sweave == Sweave + LaTeX), but LaTeX isn't the easiest thing to use or work with cross-platform, especially if you are used to MS Word etc. It is positively a pain if you need to work with people who only use MS Word. odfWeave uses Sweave, so you can learn a bit out that side of the equation, but produce documents that look like Word files and can be editted natively on any OS using OpenOffice.org. – Gavin Simpson Nov 11 '10 at 09:24
  • Can you post or point to a really simple implementation so that we can get the idea. I assume the starting point is a an r script that loads the odfWeave package, a computer running R and open office? So I guess all we need to see is a a very simple r script file? If it more complicated than that how about a couple of screen shots or video screen capture. – Farrel Nov 12 '10 at 15:58
  • @Farrel - I don't use odfWeave personally just standard Sweave with LaTeX. You need an odt document that contains noweb markup chunks. These chunks contain the R code you want evaluating. Your run the odt document through `odfWeave()` (within R) and it replaces the R code chunks with the relevant output. Grab the odfWeave package sources from CRAN and look in the inst/examples folder and open the simple.odt file in openoffice to see a simple example of the file you create before processing it with `odfWeave()`. Can't be much more help than that I'm afraid - now, if you want to talk LaTeX... – Gavin Simpson Nov 12 '10 at 16:18
4

how about the R2wd package?

eliavs
  • 2,306
  • 4
  • 23
  • 33
  • example here: http://www.r-statistics.com/2010/05/exporting-r-output-to-ms-word-with-r2wd-an-example-session/ – radek Nov 11 '10 at 19:54
4

Here is my solution. It uses Sweave with reStructuredText markup which allows easy generation of odf, Latex and HTML documents from a single source. See here for different writers included in docutils.

There is also rst2wordml writer that doesn't implement all the features of rest, but you may find it also useful. It works with the example from my blog (first link) if you remove the table of contents directive.

Matti Pastell
  • 9,135
  • 3
  • 37
  • 44
3

Also worth mentioning: the RTF package isn't as powerful as some of the other options, but it's got less of a learning curve.

janattack
  • 410
  • 3
  • 11
2

I'd recommend Emacs Org-Mode with Org-Babel and R. It means you can do R as a part of a document using literate programming with professional output to PDF (via Latex).

See:

http://orgmode.org/worg/org-contrib/babel/uses.php

http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-R.php

Demosthenex
  • 4,343
  • 2
  • 26
  • 22
2

The better solution seems to be the officer package : https://cran.r-project.org/web/packages/officer/index.html

library(officer)
doc <- read_docx() %>%
body_add_par("A title", style = "heading 1") %>%
body_add_par("Hello world!", style = "Normal") %>%
body_add_par("centered text", style = "centered")
print(doc, target = "body_add_par.docx" )
Vincent Guyader
  • 2,927
  • 1
  • 26
  • 43
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – geisterfurz007 Feb 10 '18 at 19:39
  • the question is "what's the best way to generate a word-processor file (ideally as platform-independent as possible, but native Word formats will do), with images in it, from the statistical language R?" and the answer is simply "the package officer", i put a link to the offical page on the CRAN. what can be more revelant ? Regards – Vincent Guyader Feb 10 '18 at 21:48