Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture R text+image output into one file (html, doc, pdf etc)?

Tags:

r

knitr

sweave

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.

like image 963
Tal Galili Avatar asked Sep 28 '10 21:09

Tal Galili


People also ask

How do I export R Markdown to HTML?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.


2 Answers

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:

  • Getting started with R Markdown
  • Case study in using R Markdown
like image 109
Jeromy Anglim Avatar answered Sep 18 '22 14:09

Jeromy Anglim


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

like image 22
Gavin Simpson Avatar answered Sep 22 '22 14:09

Gavin Simpson