Using tikzDevice with Sweave in R 2.13

April 12, 2011
By

(This article was first published on cameron.bracken.bz » R, and kindly contributed to R-bloggers)

R 2.13 introduces an option to specify a custom graphics device in an Sweave code chunk. This is really cool and allows you to use tikzDevice output like pgfSweave does. In an pinch, say when you dont have access to any non-core packages, you can use tikzDevice output with the regular Sweave driver (RweaveLatex) more simply than before. Here is a minimal example showing how.

\documentclass{article}

\usepackage[noae,nogin]{Sweave}
\usepackage{tikz}



\begin{document}
\SweaveOpts{prefix.string=fig}

Example using \texttt{tikzDevice} with the default \textbf{Sweave}
driver (\texttt{RweaveLatex}).  First define a custom device:

<<results=hide>>=
tikz.Swd <- function(name, width, height, …){
    require(tikzDevice)
    tikzDevice::tikz(file = paste(name, "tikz", sep = "."),
                 width = width, height = height)

}
@

Then use the device for plotting, but you have to include the figure manually.

<<myplot, fig=T, grdevice=tikz.Swd, include=F, width=4,height=4>>=
plot(1)
@
\input{fig-myplot.tikz}


\end{document
}

WARNING: This could be very slow if your graphic is complex

To leave a comment for the author, please follow the link and comment on his blog: cameron.bracken.bz » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.