## This script is a supplement to 'Conditional Visualization for Statistical ## Models: an Introduction to the condvis Package in R'. This is an example of ## interacting with a single R plot. Run the whole script. ## Function to create an event handler function to react ## to mouseclicks. Highlights a bar in a histogram. mouseclick <- function (object) { index <- NULL function (buttons, x, y) { if (!is.null(index)) { rect(xleft = object$breaks[index], xright = object$breaks[ index + 1], ybottom = 0, ytop = object$counts[index], col = "white") } index <<- which.min(abs(grconvertX(x, "ndc", "user") - object$mids)) rect(xleft = object$breaks[index], xright = object$breaks[index + 1], ybottom = 0, ytop = object$counts[index], col = "gray") } } ## Open a suitable device for interactivity. if (identical(version$os, "linux-gnu")) { x11(type = "Xlib") } else { x11() } o <- hist(mtcars$mpg) ## Set up the event handler and listen for events. setGraphicsEventHandlers(onMouseDown = mouseclick(o)) getGraphicsEvent() ## Click on the histogram to highlight bars.