Embedding R Code in LaTeX Using tikzDevice Package

LaTeX/Sweave integration is awesome! Dynamic reporting made easy indeed. Integrating the overall workflow with tikZ functionality right within R is even better as it esures that your plots have a consistent look and feel through out your report.

Here is an example workflow that show how one would normally go about achieving this…

Sweave Code

The code below can be run right within the R console or using the R CMD Sweave command.

<<fig=FALSE, echo=FALSE>>=

# load required libraries and define environment
require('tikzDevice')

# use the tikz() function to generate a Tex file with native tikZ code
# standAlone=FALSE - I want direct processing with LaTeX
# bareBones=FALSE - I want output to be wrapped right within LaTeX tikzpicture environment
# width=4.5, height2.5 - I want specific dimensions for my plot
tikz('ux-evaluation.tex', standAlone=FALSE, bareBones=FALSE,
 width=4.5, height=2.5)

# define external data source
# this is irrelevant in this context as there are various ways of plotting
bonoloui <- read.table("bonoloui.dat", header=T, sep="\t")

# define plot area
par(xpd=T, mar=par()$mar+c(0,0,0,3), cex=0.6, font=2)
barplot(as.matrix(bonoloui), beside=TRUE, xlab="Average UX Ratings",
 ylab="Subjects",
 ylim=c(0,6), col=rainbow(7), las=1,
 space=c(0, 3), names.arg=
 c("Search and Browse", "Navigability",
 "Similarity", "General"), axes=FALSE, ann=FALSE, font.lab=2)
axis(2, las=1, at=(seq(0,5, by=1)))
legend(38,5, c("Intuitive", "Simple",
 "Satisfied", "Expected",
 "Response", "Effective", "Beneficial"),
 fill=rainbow(7), bty="o", text.width=5);
abline(h=(seq(0,5,0.5)), col="lightgray", lty="dotted")
@

The Sweave code above can be run within the *nix shell or right within R and shown below.

scap@scap-lenovo-g560:~$ R CMD Sweave ux-evaluation.Rnw
Writing to file ux-evaluation.tex
Processing code chunks with options ...
 1 : keep.source term verbatim
Loading required package: tikzDevice
Loading required package: filehash
filehash: Simple key-value database (2.2-1 2012-03-12)
tikzDevice: R Graphics Output in LaTeX Format (v0.6.2-92-0ad2792)
 LaTeX found in the PATH using the command: pdflatex
 XeLaTeX found in the PATH using the command: xelatex
 LuaLaTeX found in the PATH using the command: lualatex
Creating temporary TikZ metrics dictionary at:
 /tmp/RtmpRmUbt5/tikzMetricsDictionary

 

> Sweave('ux-evaluation.Rnw')
Writing to file ux-evaluation.tex
Processing code chunks with options ...
 1 : keep.source term verbatim
Loading required package: tikzDevice
Loading required package: filehash
filehash: Simple key-value database (2.2-1 2012-03-12)
tikzDevice: R Graphics Output in LaTeX Format (v0.6.2-92-0ad2792)
 LaTeX found in the PATH using the command: pdflatex
 XeLaTeX found in the PATH using the command: xelatex
 LuaLaTeX found in the PATH using the command: lualatex
Creating temporary TikZ metrics dictionary at:
 /tmp/Rtmpg9oibh/tikzMetricsDictionary

You can now run (pdf)latex on ‘ux-evaluation.tex’

LaTeX Integration

\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
:
\begin{document}
:
\begin{figure}[ht]
 \centering
 \framebox[\textwidth] {
 \input{ux-evaluation.tex}
 }
 \caption{An R Barplot Integrated with \LaTeX}
 \label{r-barplot}
\end{figure}
:
\end{document}
\end{lstlisting}
\end{document}

Final Output


I hope this helps someone out there. Leave a comment if you know of an easier way of performing certain tasks above.

Bibliography

[1] http://cran.csdb.cn/web/packages/tikzDevice/tikzDevice.pdf