### R code from vignette source 'murrell-potter.Rnw' ################################################### ### code chunk number 1: murrell-potter.Rnw:20-21 ################################################### options(continue=" ") ################################################### ### code chunk number 2: murrell-potter.Rnw:101-102 ################################################### library(grid) ################################################### ### code chunk number 3: monty-setup ################################################### text <- sample(c("goat", "goat", "car")) cols <- hcl(c(0, 120, 240), 80, 80) ################################################### ### code chunk number 4: murrell-potter.Rnw:129-131 ################################################### # Fix the order of the text so I can rely on it in screenshots, etc text <- c("car", "goat", "goat") ################################################### ### code chunk number 5: monty-fun ################################################### MontyHall <- function() { grid.newpage() grid.text(text, 1:3/4, gp=gpar(cex=2), name="prizes") for (i in 1:3) { grid.rect(i/4 - .1, width=.2, height=.8, just="left", gp=gpar(fill=cols[i]), name=paste0("door", i)) } } ################################################### ### code chunk number 6: montyhall ################################################### MontyHall() ################################################### ### code chunk number 7: murrell-potter.Rnw:161-162 (eval = FALSE) ################################################### ## grid.ls() ################################################### ### code chunk number 8: murrell-potter.Rnw:164-166 ################################################### MontyHall() grid.ls(full=TRUE) ################################################### ### code chunk number 9: murrell-potter.Rnw:185-186 ################################################### library(gridSVG) ################################################### ### code chunk number 10: hyperlink (eval = FALSE) ################################################### ## links <- c("http://www.google.com/search?q=car&tbm=isch", ## "http://www.google.com/search?q=goat&tbm=isch") ## for (i in 1:3) { ## grid.hyperlink(paste0("door", i), ## href=links[match(text[i], c("car", "goat"))]) ## } ################################################### ### code chunk number 11: export-hyper (eval = FALSE) ################################################### ## grid.export("montyhall-hyper.svg") ################################################### ### code chunk number 12: murrell-potter.Rnw:210-214 ################################################### gridsvg("montyhall-hyper.svg", width=6, height=3, prefix="hyper-") MontyHall() links <- c("http://www.google.com/search?q=car&tbm=isch", "http://www.google.com/search?q=goat&tbm=isch") for (i in 1:3) { grid.hyperlink(paste0("door", i), href=links[match(text[i], c("car", "goat"))]) } dev.off() ################################################### ### code chunk number 13: montyhall-anim (eval = FALSE) ################################################### ## MontyHall() ## goatDoor <- grep("goat", text)[1] ## grid.animate(paste0("door", goatDoor), width=c(.2, 0), duration=2) ################################################### ### code chunk number 14: murrell-potter.Rnw:248-249 (eval = FALSE) ################################################### ## grid.export("montyhall-anim.svg") ################################################### ### code chunk number 15: murrell-potter.Rnw:251-254 ################################################### gridsvg("montyhall-anim.svg", width=6, height=3, prefix="anim-") MontyHall() goatDoor <- grep("goat", text)[1] grid.animate(paste0("door", goatDoor), width=c(.2, 0), duration=2) dev.off() ################################################### ### code chunk number 16: murrell-potter.Rnw:256-261 ################################################### # Version for screen cap (end with door half open) gridsvg("montyhall-anim-cap.svg", width=6, height=3) MontyHall() grid.animate(paste0("door", goatDoor), width=c(.2, .1), duration=2) dev.off() ################################################### ### code chunk number 17: mask ################################################### circleMask <- gTree(children=gList(rectGrob(gp=gpar(col=NA, fill="white")), circleGrob(x=goatDoor/4, r=.15, gp=gpar(col=NA, fill="grey")), polylineGrob(c(0, 1, .5, .5), c(.5, .5, 0, 1), id=rep(1:2, each=2), gp=gpar(lwd=10, col="white")))) ################################################### ### code chunk number 18: mask-fig ################################################### grid.draw(circleMask) grid.rect(gp=gpar(col="grey")) ################################################### ### code chunk number 19: masked (eval = FALSE) ################################################### ## MontyHall() ## grid.mask(paste0("door", goatDoor), mask(circleMask)) ################################################### ### code chunk number 20: murrell-potter.Rnw:322-323 (eval = FALSE) ################################################### ## grid.export("montyhall-masked.svg") ################################################### ### code chunk number 21: murrell-potter.Rnw:326-329 ################################################### gridsvg("montyhall-masked.svg", width=6, height=3, prefix="masked-") MontyHall() grid.mask(paste0("door", goatDoor), mask(circleMask)) dev.off() ################################################### ### code chunk number 22: tooltip (eval = FALSE) ################################################### ## MontyHall() ## for (i in 1:3) { ## grid.garnish(paste0("door", i), title=text[i]) ## } ################################################### ### code chunk number 23: murrell-potter.Rnw:376-377 (eval = FALSE) ################################################### ## grid.export("montyhall-tooltip.svg") ################################################### ### code chunk number 24: murrell-potter.Rnw:379-382 ################################################### gridsvg("montyhall-tooltip.svg", width=6, height=3, prefix="tooltip-") MontyHall() for (i in 1:3) { grid.garnish(paste0("door", i), title=text[i]) } dev.off() ################################################### ### code chunk number 25: alert (eval = FALSE) ################################################### ## MontyHall() ## for (i in 1:3) { ## grid.garnish(paste0("door", i), ## onclick=paste("alert('", text[i], "')")) ## } ################################################### ### code chunk number 26: murrell-potter.Rnw:413-414 (eval = FALSE) ################################################### ## grid.export("montyhall-alert.svg") ################################################### ### code chunk number 27: murrell-potter.Rnw:416-419 ################################################### gridsvg("montyhall-alert.svg", width=6, height=3, prefix="alert-") MontyHall() for (i in 1:3) { grid.garnish(paste0("door", i), onclick=paste("alert('", text[i], "')")) } dev.off() ################################################### ### code chunk number 28: js ################################################### MontyHall() for (i in 1:3) { grid.garnish(paste0("door", i), onclick="open(evt)") } grid.script(file="MontyHall.js") ################################################### ### code chunk number 29: murrell-potter.Rnw:451-452 (eval = FALSE) ################################################### ## grid.export("montyhall-js.svg") ################################################### ### code chunk number 30: murrell-potter.Rnw:454-457 ################################################### gridsvg("montyhall-js.svg", width=6, height=3, prefix="js-") MontyHall() for (i in 1:3) { grid.garnish(paste0("door", i), onclick="open(evt)") } grid.script(file="MontyHall.js") dev.off()