Skip to content

variable name substitution

8 messages · Duncan Murdoch, Ivan Calandra, Carlos Ortega

#
On 18/01/2010 9:02 AM, Ivan Calandra wrote:
I think you're on the wrong path.  You want to write a function, and 
pass either x and y as arguments, or pass a formula containing both (the 
former is easier).  For example,

twoplots <- function(x, y) {
  ylab <- deparse(substitute(y))  # get the expression passed as y
  xlab <- deparse(substitute(x))  # get the expression passed as x
  hist(y, main=paste("Histogram of ", ylab), xlab=ylab)
  boxplot(y ~ x,  main=paste("Boxplot of", ylab, "by", xlab), xlab=xlab, 
ylab=ylab)
}

Then

with(ssfa, twoplots(TO_POS, Asfc))

will give you your plots.

Duncan Murdoch
1 day later