**Revolutions**, and kindly contributed to R-bloggers)

We've mentioned before how you can use R to design 3-D objects. Now, thanks to the latest version of the rgl package, you can produce real-world 3-D objects with R as well.

The rgl package has long made it possible to create virtual 3-D objects in R, and export them as animations like this:

But now, package author Duncan Murdoch has added the ability to export such 3-D objects in formats used by 3-D printers (such as STL, WebGL, PLY, and OBJ). Duncan used this feature to create the corresponding real-world sculpture below:

Duncan explained how he obtained the 3-D print via email:

I ordered it from shapeways.com, a service bureau that does 3D printing in a variety of materials. (They're in NYC, and were shut down for a few days by Hurricane Sandy, but still managed to get my print to me a day earlier than they promised to ship it!) Including shipping to Canada, the cost was $22, and I had the print 11 days after I wrote the software.

The 3-D object file (in STL format) was created using just a few R commands and some elegant mathematics:

## Install rgl from R-forge with ## install.packages("rgl", repos="http://R-Forge.R-project.org") library(rgl) theta <- seq(0, 6*pi, len=2048) sawtooth <- function(x) sin(x) + sin(2*x)/2 dsawtooth <- function(x) cos(x) + cos(2*x) fb <- 21+1/3 x1 <- sawtooth(fb*theta) dx1 <- dsawtooth(fb*theta) offset <- 3*pi/3 y1 <- sawtooth(fb*theta - offset ) dy1 <- dsawtooth(fb*theta - offset) r1 <- 0.1 r2 <- 0.4 k <- cylinder3d(cbind(sin(theta)+2*sin(2*theta), 2*sin(3*theta), cos(theta)-2*cos(2*theta)), e1=cbind(cos(theta)+4*cos(2*theta), 6*cos(3*theta), sin(theta)+4*sin(2*theta)), radius=0.8, closed=TRUE, keepVars=TRUE) knot <- attr(k, "vars") center <- cbind(0, x1, y1)*r2 e1 <- cbind(1, dx1, dy1)*r2 e2 <- cbind(rep(0, length(theta)), 1, 0) for (i in 1:length(theta)) { trans <- cbind(knot$e1[i,], knot$e2[i,], knot$e3[i,]) center[i,] <- knot$center[i,] + trans %*% center[i,] e1[i,] <- trans %*% e1[i,] e2[i,] <- trans %*% e2[i,] } braid <- addNormals(cylinder3d(center, e1=e1, e2=e2, radius=r1)) shade3d(braid, col="white") writeSTL("braid.stl")

*Created by Pretty R at inside-R.org*

Try it yourself! The package installed easily for me on a Mac running R 2.15.2, and I could also rotate the 3-D object interatively on-screen using the shade3d function. For more info, visit the rgl progect page linked below.

r-forge: rgl project page

**leave a comment**for the author, please follow the link and comment on his blog:

**Revolutions**.

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