How do I generate a document (.rtf, .doc, .odt) from R
Asked Answered
C

6

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?

Carnotite answered 10/11, 2010 at 16:32 Comment(1)
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.Carnotite
P
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.

Product answered 10/11, 2010 at 16:51 Comment(6)
odfWeave is probably what you are looking for.Hooch
Thanks! odfweave looks like the winner here.Carnotite
It sure does, but looking into Sweave is really worth it too if you can afford the time.Paratuberculosis
@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.Product
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.Interrogation
@Interrogation - 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...Product
H
4

how about the R2wd package?

Henceforth answered 10/11, 2010 at 18:15 Comment(1)
example here: r-statistics.com/2010/05/…Pori
V
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.

Vault answered 10/11, 2010 at 18:31 Comment(0)
F
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.

Francium answered 7/6, 2013 at 17:6 Comment(0)
R
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

Religiosity answered 10/11, 2010 at 17:59 Comment(0)
P
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" )
Persuasion answered 10/2, 2018 at 15:47 Comment(2)
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link 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.Barrault
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 ? RegardsPersuasion

© 2022 - 2024 — McMap. All rights reserved.