June 17, 2015

Outline

  1. Sweavetest (Sweave source)
  2. rgl (knitr markdown source)
  3. References

Sweavetest

rgl

  • rgl is an R package that uses OpenGL and WebGL to display 3D graphics. Daniel Adler and I first released it in 2002.
  • OpenGL is a library that's about 25 years old for rendering graphics on computer screens; WebGL is a recent Javascript interface to it, now supported in most browsers.
  • rgl includes fairly high-level functions modelled on classic R graphics, as well as low-level functions for dealing with OpenGL. Users don't need to know anything about OpenGL to use it.

rgl in Teaching

  • I use rgl in my "Introduction to Statistical Computing" course, though writing code using it is a little complex for most of the students at that level.
  • It is useful for displaying statistical computing concepts.
  • The recent WebGL support makes this easier, because knitr and rmarkdown (Allaire et al. 2014) make it easy to embed rgl graphics on a web page. (This half of the talk is written that way.)
  • Instead of LaTeX, the underlying language is Markdown, with code chunks similar to Sweave.

Example: Perspective plot of surface

  • persp3d displays a general parameterized surface:
z <- matrix(seq(0, 1, len=50), 50, 50)
theta <- t(z)
r <- 1 + exp( -pmin( (z - theta)^2, 
             (z - theta - 1)^2, 
             (z - theta + 1)^2 )/0.01 )
x <- r*cos(theta*2*pi)
y <- r*sin(theta*2*pi)

persp3d(x, y, z, col="red")

persp3d Output

You must enable Javascript to view this page properly.

Example: clipping to show inside a shape

First, draw a sphere. Put a bitmapped "texture" on the surface.

lat <- matrix(seq(90,-90, len=50)*pi/180, 50, 50, byrow=TRUE)
long <- matrix(seq(-180, 180, len=50)*pi/180, 50, 50)

r <- 6378.1 # radius of Earth in km
x <- r*cos(lat)*cos(long)
y <- r*cos(lat)*sin(long)
z <- r*sin(lat)

obj <- persp3d(x, y, z, col="white", 
       texture=system.file("textures/world.png",package="rgl"), 
       specular="black", axes=FALSE, box=FALSE, 
       xlab="", ylab="", zlab="",
       normal_x=x, normal_y=y, normal_z=z)

You must enable Javascript to view this page properly.

Output of full sphere