embed images in Rd documents

April 3, 2010
By

(This article was first published on Romain Francois, Professional R Enthusiast, and kindly contributed to R-bloggers)

The new help system that was introduced in R 2.10.0 and documented in an article of the R journal is very promising.

One thing that is planned for future versions of R (maybe 2.12.0) is some way to include images into Rd documents using the fig option of the Sexpr macro

Another way is to use data uri and embed the image directly inside the html code, so this morning I played with this and wrapped up this little c library into an R package called base64 and hosted in the Rcpp project at r-forge.

The package allows encoding and decoding files using the Base64 format. It currently has three functions: encode, decode and img. encode and decode do what their name implies, and img produces the html code suitable for embedding the image into an html document.

The help page for img actually contains an image, here is it:

and here is how it is produced:

\details{
\if{html}{
	The following graph is embedded in the document using the \code{img} function	
	
\Sexpr[stage=render,results=rd,echo=FALSE]{
	library( base64 )
	library( grDevices )
	library( graphics )
	library( stats )
	
	pngfile <- tempfile()
	png( pngfile, width = 600, height = 400 )
	plot( 1:100, rnorm(100), pch = 21, bg = "red", cex = 2 )
	dev.off()
	img( pngfile, Rd = TRUE )
}
}

}

To leave a comment for the author, please follow the link and comment on his blog: Romain Francois, Professional R Enthusiast.

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, 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...

Tags: , , ,

Comments are closed.