20

The task is to create a file (word, rtf, pdf, html, or whatever) that will capture the output of R (e.g: not the code that created the output), into that format (including text and images).

The way of doing this should involve as little change to the original R script as possible.

If I had cared only for the text or images, then I would use ?sink, or ?pdf. But I don't know how to combine the two into one output in an easy way.

I know there is a way to export R output using r2wd, but it involves too much medaling in the original code for my taste (I imagine the same is true for the sweave solution, although I don't have experience with it to tell)

Here is a sample code for future examples:

START.text.and.image.recording("output.file") # this is the function I am looking for
x <- rnorm(100)
y <- jitter(x)
print(summary(x))
print(head(data.frame(x,y)))
cor(x,y)
plot(x,y)
print(summary(lm(y~x)))
STOP.text.and.image.recording("output.file") # this is the function I am looking for

Update: I was asked way not Sweave, or other options from ReproducibleResearch task view.

The reasons are:

  1. I don't (yet) know LaTeX
  2. Even knowing LaTeX, I want something with simple defaults to simply dump all the outputs together, and in order. "simply" means - as little extra code/file management overhead as possible.

I understand that something like sweave or brew are more scalable, but I am looking to see if there is a more "simple" solution for smaller projects/scripts.

Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
Tal Galili
  • 24,605
  • 44
  • 129
  • 187

11 Answers11

15

As of 2012 knitr provides a perfect solution to this problem.

For example, create a file with an rmd extension. Wrap your code in a couple of commands as follows:

```{r}
x <- rnorm(100)
y <- jitter(x)
print(summary(x))
print(head(data.frame(x,y)))
cor(x,y)
plot(x,y)
print(summary(lm(y~x)))
```

You can convert it into a self-contained HTML file in several ways. In RStudio you just press a single button Knit HTML. This is the HTML file produced; to actually view how the HTML displays in a browser, save the file and open it.

Images code, and output are interweaved as you might expect.

Of course, you can and typically would divide up your file into multiple R code chunks. But the point is, you don't have to.

Here are another couple of examples I've created:

Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
5

If you know LaTeX, sweave will likely be your best bet. odfWeave is a similar mechanism but for embedding the code in an OpenOffice.org file. For HTML there is the R2html package. But all will likely require you to break the code up a little bit to get the best out of the systems. Alternatively, your sweave/odfweave/html template could source the data generation aspects of the script in a single code chunk, with the output display (print() statements) placed where required. Your graphics could also be called within the script to produce the figures to embed in the document as separate files, which you then include by hand in the template.

For example (and this isn't a full .Rnw file for running through sweave) in a sweave file you'd put something like this high up in the template which sources the main part of the R script that will do the analysis and generate the R objects:

<<run_script, eval=TRUE, echo=FALSE, results=hide>>=
source("my_script.R")
@

Then you will need to insert code chunks where you want printed output:

<<disp_output, eval=TRUE, echo=FALSE, results=verbatim>>=
## The results=verbatim is redundant as it is the default, as is eval=TRUE
print(summary(x)) ## etc
@

Then you will need chunks to insert the figures.

Separating your analysis code from the output (printed and/or figures) is probably good practice as well, especially if the analysis code is expensive in compute terms. You can run it once - or even cache it - whilst updating the output/display code as you need to.

Example Sweave File

Using csgillespie's example sweave file I would set things up like this. First the my_script.R file containing the core analysis code:

x <- rnorm(100)
y <- jitter(x)
corXY <- cor(x,y)
mod.lm <- lm(y~x)

Then the Sweave file

\documentclass[12pt]{article}
\usepackage{Sweave}
\begin{document}

An introduction
<<run_analysis, eval=TRUE,echo=FALSE, results=hide>>=
source("my_script.R")
@

% Later
Here are the results of the analysis
<<show_printed_output, echo=FALSE>>=
summary(x))
head(data.frame(x,y))
@

The correlation between \texttt{x} and \texttt{y} is:
<<print_cor, echo=FALSE>>=
corXY
@

Now a plot
\begin{figure}[h]
    \centering
<<echo=FALSE, eval=TRUE, fig=TRUE, width=6, height=4>>=
plot(x,y)
@ 
\caption{\textit{A nice plot.}}
\end{figure}

\end{document}

What you seem to be wanting doesn't exist; a simple way of combining R code and output into a document file. That is if you don't consider sweave and its ilk simple. You might need to rethink what you want to do or how you arrange your analysis and graphics and output code, but you are likely best served looking at one of the suggested options (sweave, odfweave, brew, R2html).

HTH

Community
  • 1
  • 1
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
5

I would encourage you to use Sweave, but a rudimentary functionality that is not pretty can be achieved with sink().

A regular txt file:

sink(file = "test.txt", type = "output")
summary(cars)
sink()

or add some HTML tags:

sink(file = "tal_test.html", type = "output")
cat("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"", "\n")
cat("\"http://www.w3.org/TR/html4/strict.dtd\">", "\n")
        cat("<HTML>", "\n")
        cat("<HEAD>", "\n")
        cat("<TITLE>My first HTML document</TITLE>", "\n")
        cat("</HEAD>", "\n")
        cat("<BODY>", "\n")
        summary(cars)
        cat("</BODY>", "\n")
        cat("</HTML>", "\n")
sink()
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
4

I wrote a script called Roux about a year ago which does this. I wanted to be able to create HTML transcripts from running an R script, including any images, without having to change the script.

You call Roux from the command line, like this: roux example.R

and roux will:

  • run the script in R (requiring the Roux package first automatically)
  • syntax highlight the .Rout output using Pygments
  • insert images in the correct location

the Roux R package is a very small R package which modifies plot() and some other functions to automatically write to a random filename rather than the default interactive graphics device.

I have used this a lot, and it works really well for me, although I'm sure if more people use it with new packages then minor issues will arise, most likely that you'll have a different function which generates a graph and Roux won't know that it should open a PNG device for you.

Since speaking with Tal about this I have updated and improved the code, and it's now up here: http://bitbucket.org/ananelson/roux/src

so if you run into any issues, please report them to the issue tracker there on Bitbucket.

I have added support for LaTeX transcripts so you can easily create PDFs which have the transcript of your R script including images. (You can see an example if you look in the example-output directory, find the "raw" link to download it.)

You do need to have Python and the Pygments python library intalled. If you have an older version of Python and run into any issues, please let me know.

I wrote about Roux on my blog but didn't publicize it that much because my efforts have been focused on a new project called Dexy which is intended as a replacement for Sweave. If you want more flexibility and control or are interested in literate documentation then you might want to check out Dexy too.

2

You mentioned sweave in your question but not really why it isn't suitable. Your question seems perfect for Sweave. In fact, your example code could have came from the second Sweave example.

Example Sweave file

If you know Latex then Sweave isn't that difficult. Here's your example file as a Sweave file:

\documentclass[12pt,BCOR3mm,DIV16]{scrreprt}
\usepackage{Sweave}
\begin{document}

An introduction
<<eval=TRUE,echo=TRUE>>=
x <- rnorm(100)
y <- jitter(x)
print(summary(x))
print(head(data.frame(x,y)))
cor(x,y)
@ 
Now a plot
\setkeys{Gin}{width=0.5\textwidth}
\begin{figure}[h]
    \centering
<<echo=FALSE, eval=TRUE, fig=TRUE, width=6, height=4>>=
plot(x,y)
@ 
\caption{\textit{A nice plot.}}
\end{figure}

\end{document}

Under linux, just save the file as tmp.Rnw. Then

R CMD Sweave tmp.Rnw
pdflatex tmp.tex
csgillespie
  • 59,189
  • 14
  • 150
  • 185
  • 1
    Hi csgillespie, the code for the example you gave has a lot of "before and after" additions to the code (http://www.stat.uni-muenchen.de/~leisch/Sweave/example-2.Snw) including the fact that I will need to learn (some) latex+sweave. Which is something to do one day, but I am looking if there is a simpler alternative. – Tal Galili Sep 28 '10 at 22:03
  • @Tal I'll add to @csgillespie `odfSweave` and `brew` (probably less flexible). Check out also *Sweave and LaTeX*, http://j.mp/dah7Hr. – chl Sep 28 '10 at 22:12
  • 2
    @Tal I guess you can always write some code (Python, Ruby or whatever) to store R output in temp file, as well as images, calling R in BATCH mode, and then compile all of them in a tex file after inserting the temp files. Actually, this is what is done for embedding R code in ConTeXt file. – chl Sep 28 '10 at 22:22
  • @Tal Huh, I browse through your `r2wd` example: it's awful (ok, I never use Micro$oft tools, and my appreciation is certainly biased)! I'm sure there's a better way to handle this with Python scripting and tex/html export... Just to call for other responses... – chl Sep 28 '10 at 22:37
  • Hi chl, I have seem to have found a solution doing what you are describing! Please have a look at the answer I just posted. Best, Tal – Tal Galili Oct 02 '10 at 08:23
2

There is also LyX, which has an Sweave interface. The R / LyX / Sweave interface code is on CRAN at http://cran.fhcrc.org/contrib/extra/lyx/. LyX itself is in most of the Linux distros. All of this magic can be made to work on Windows, but it's definitely non-trivial. On Windows, I'd recommend Inference for R from Blue Reference for literate R progamming.

2

Well, I just remind that I was using Asciidoc for short reporting or editing webpage. Now there's an R plugin (ascii on CRAN), which allows to embed R code into an asciidoc document. The syntax is quite similar to Markdown or Textile, so you'll learn it very fast.

Output are (X)HTML, Docbook, LaTeX, and of course PDF through one of the last two backends.

Unfortunately, I don't think you can wrap all your code into a single statement. However, it supports a large number of R objects, see below.

> methods(ascii)
 [1] ascii.anova*              ascii.aov*                ascii.aovlist*            ascii.cast_df*           
 [5] ascii.character*          ascii.coxph*              ascii.CrossTable*         ascii.data.frame*        
 [9] ascii.default*            ascii.density*            ascii.describe*           ascii.describe.single*   
[13] ascii.factor*             ascii.freqtable*          ascii.ftable*             ascii.glm*               
[17] ascii.htest*              ascii.integer*            ascii.list*               ascii.lm*                
[21] ascii.matrix*             ascii.meanscomp*          ascii.numeric*            ascii.packageDescription*
[25] ascii.prcomp*             ascii.sessionInfo*        ascii.simple.list*        ascii.smooth.spline*     
[29] ascii.summary.aov*        ascii.summary.aovlist*    ascii.summary.glm*        ascii.summary.lm*        
[33] ascii.summary.prcomp*     ascii.summary.survfit*    ascii.summary.table*      ascii.survdiff*          
[37] ascii.survfit*            ascii.table*              ascii.ts*                 ascii.zoo*               

   Non-visible functions are asterisked
chl
  • 27,771
  • 5
  • 51
  • 71
2

This is in light of romunov's answer, but still. You can just write your own print that wraps the output in some HTML formatting and embeds the output to a HTML file. The same can be done with pictures with Data URI scheme, for instance by using img function from base64 R package.

mbq
  • 18,510
  • 6
  • 49
  • 72
  • Hi mbq, do you mean to wrap every line of code, or to wrap the entire section ? – Tal Galili Sep 29 '10 at 11:25
  • @Tal Rather a custom `print` and `plot` that formats ans redirect their output to the report file. R does not have any output handler to hook to :-( – mbq Sep 29 '10 at 11:33
  • @mbq Isn't this close to what `R2HTML` actually does (more or less)? – chl Sep 29 '10 at 11:37
  • @chl R2HTML is R object to HTML converter, Tal wanted to capture textual output and images only, to, as I understand, make something like static Mathematica or Sage notebook. – mbq Sep 29 '10 at 11:51
  • mbq: textual output includes tables/data.frames/lm.summary and so on. It is true that outputting it as text (ascii) is fine. Although the better solution would be something like R2HTML, but without the need to add all those lines of code around **every** output statement. – Tal Galili Sep 29 '10 at 12:03
  • @Tal BTW, with `ascii` you not only get a readable plain text file but also an HTML page (asciidoc will format the txt file); less powerful than Sweave, but you work with just a text file, so... – chl Sep 29 '10 at 12:07
  • Hi chl, thanks - the ascii package does look nice. Though I still hope for a simpler solution for smaller snippets of code :) – Tal Galili Sep 29 '10 at 12:16
  • 1
    @chl Conversion is one thing, catching output (especially images) is something other. @Tal Again -- you'll not find it, print and plot cannot be hooked in any low-level enough way. – mbq Sep 29 '10 at 12:36
  • 1
    @mbq (+1) Yes, I agree. This is why I initially suggested to rely on an external scripting language to call R in batch mode; now, if we look for a simpler solution, I do not know how we can do that. – chl Sep 29 '10 at 12:42
2

You can use the R2HTML package to output a session to html and there are some similar functions in the TeachingDemos package (see txtStart) for output to enhanced text and word (via R2wd). Non-graphics commands will be included in the file automatically and the current plot can be inserted by a single command.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
1

Through the wonders of twitter, someone reached out and sent me a link to this page, regarding a package called "roux". It was created a year ago, and I have never heard about it (apparently neither have most of you).

This package seems to do exactly what I was looking for in my question, although the installation seems non trivial.

I hope to play with this solution and also to see if other R members might go into this project to better enhance R.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Tal Galili
  • 24,605
  • 44
  • 129
  • 187
  • Oh, I seem to remember browsing the webiste some time ago, but definitively forgot about it. It remind me of the technique used on http://bm2.genes.nig.ac.jp to display complete R output. Let us know if you can manage to only display the output (text+img) and not the R command... – chl Oct 02 '10 at 08:34
  • Hi chl, I already contacted the author of that plugin. I hope that this could be accomplished (and actually much more). More (I hope) to come... – Tal Galili Oct 02 '10 at 10:08
  • Hi chl, regarding the link you gave, I forget about it. Interestingly enough, it doesn't solve the issue, since he has all the ?sink output and then all the ?png output (I am not sure if that is how he implements it, but that's the result). This indeed could be a good idea for upgrading such a website. – Tal Galili Oct 02 '10 at 10:15
0

good suggestion by @znmeb to try Lyx - a more word-like front end for LaTeX, and as the documentation points out, there is a good article of its use with Sweave on page 2 of this edition of R news

This is how I did it in Ubuntu 10.04 follwoing the guidelines in the lyx sweave repository:

sudo apt-get install lyx
cd ~./lyx
wget http://cran.fhcrc.org/contrib/extra/lyx/preferences
cd layouts
wget http://cran.fhcrc.org/contrib/extra/lyx/literate*
wget http://cran.fhcrc.org/contrib/extra/lyx/literate-article.layout
wget http://cran.fhcrc.org/contrib/extra/lyx/literate-book.layout
wget http://cran.fhcrc.org/contrib/extra/lyx/literate-report.layout
wget http://cran.fhcrc.org/contrib/extra/lyx/literate-scrap.inc
cd ~/texmf/tex
wget http://www.biostat.jhsph.edu/~rpeng/ENAR2009/Sweave.sty
  1. start Lyx
  2. Preferences -> Reconfigure
  3. restart Lyx
  4. File -> new
  5. Document -> Settings -> Document Class -> article (Sweave noweb)

useful links:

David LeBauer
  • 31,011
  • 31
  • 115
  • 189