Embedding R output in other documents

Statistical data analysis is usually followed by a written report describing the problem, the analysis done and conclusions drawn from it. It is often useful to include graphics output from R in such documents. R has several graphics ``devices'' specifically for this purpose.

Overview of Graphics devices

All R graphics output has to go to a so-called Graphics Device. There are two main types of devices:

The screen device on Windows can be invoked by calling windows() (similarly x11() on UNIX-like systems and quartz() on Macs). If an attempt is made to produce any graphics without an already open device, this device is automatically opened (well, technically, the device given by getOption("device") is opened, and this can be changed if necessary).

For functions that open file devices, the most important argument is called file, which specifies the name of the output file. For more details about specific devices, see the help page for the respective device functions. A list of device functions are given in help(Devices).

All file devices must be closed by calling dev.off(). Screen devices can be closed similarly , this has the effect of simply closing the window.

Multiple devices can be open at a time. Typically, the one opened last is ACTIVE (i.e., graphics are drawn to it). Calling dev.off() closes this ACTIVE device. The current device can be identified by the variable .Device, which has the value "null device" when no device is open.

Bitmap Image formats (JPEG, PNG)

Perhaps the most familiar types of graphics output files are bit-mapped images, as in JPEG or PNG files. This is done using the functions jpeg() and png() respectively. This sort of output is typically used for including graphics in web-pages.

Vector formats for printable documents

Bitmap formats are not suitable when there is the possibility of graphs being scaled (either when being viewed on-screen or after printing). This is usually the case for Word documents or Powerpoint presentations. For such use, the appropriate type of output devices to use are those that produce so called vector graphics, where the output file contains the plot information as a collection of geometric objects and text, and not pixels as in bitmap formats.

The most popular of these formats (on Windows) is the Windows Metafile format. The following example produces a scatter plot using the iris data in a file called iris.wmf

data(iris)
win.metafile(file = "iris.wmf")
plot(Sepal.Length ~ Sepal.Width, data = iris)
dev.off()

Once this is done, the file iris.wmf can be included in a Word or Powerpoint file.

Another popular vector format is PDF, which can be included in other documents, as well as viewed or printed directly.

Copying screen output

Often one painstakingly builds up a plot on-screen (perhaps using interactive tools, which would be impossible in file devices), and simply want the output to be copied over to a file. This is fairly easy using dev.print(), the first argument to which is the name of a graphics device function to use. Arguments to the device function can also be supplied here. Sample usage:

data(iris)
plot(Sepal.Length ~ Sepal.Width, data = iris) ## plots on-screen
title(main = "Iris Sepal Measurements") ## adds title
dev.print(win.metafile, file = "iris.wmf")

A shortcut (on Windows) is to right click on the on-screen graph and choose the appropriate device.

This is a nice feature, but is not recommended for simple plots, where plotting to an explicitly opened device will do the work just as well. There are two reasons for this:

Embedding non-graphics output

Typically text output from R is pasted as it is into other documents (see ?source and ?sink for reading input from and redirecting output to a file). Remember that R uses a fixed width font, and uses that knowledge to do all its formatting. So use a fixed width font when you paste R output in a Word document (Andale Mono is a good choice).

Sometimes, it is useful to be able to convert tables in R to a more flexible format. I don't know of any way to generate tables suitable for inclusion directly in a Word document. However, the R2HTML package contributed by Eric Lecoutre has the ability to send R output to an HTML file, which can (I think) then be converted into Word. For example, here's what you would get in R:

> data(Titanic)
> Titanic[,,2,1]
      Sex
Class  Male Female
  1st   118      4
  2nd   154     13
  3rd   387     89
  Crew  670      3

Here's the same table converted to HTML using the R2HTML package:

MaleFemale
1st1184
2nd15413
3rd38789
Crew6703

Ask me for more details if you want to use this package.

Embedding R output in LaTeX documents

The most popular typesetting software in the mathematics and statistics community is LaTeX. R has a very useful package, written by Friedrich Leisch, that provides support for literate programming using LaTeX and R (literate programming is a style of writing documentation interspersed with code --- where the code is actually run as part of the document generation cycle).

An introduction to this system can be found here. All the PDF slides available here were created using this system. If you are interested, you can take a look at the .Rnw files linked from the main course page.

Last modified: Mon Feb 1 14:15:17 IST 2010