[R] substitute() reading data instead of name

Carl Witthoft carl at witthoft.com
Tue Jun 10 02:38:29 CEST 2008

I seem to have run across a bug in which substitute() inside a function
definition gets 'confused.'  The code is listed below.
The same behavior occurs under OSX 10.3.9, PPC,  w/ R2.2 and Rgui 1.14
and under OSX 10.4.11 Intel w/ 2.70 and the latest Rgui.

What I see is that 'xlab' properly has the name of the data I entered
for the x-input.  But 'ylab' contains the string '  c( {all the data
values in the y-input vector}) '  .
If I un-comment the 'cat(ylab)' line or the 'dreck<-y' line, or if I
create yy<-y and use yy from there on out,  everything works properly.

So, is this a bug, or am I a foolish n00b, or do I need to "flush"

thanks for your help.

Here is the function .
# sample of odd behavior when  using deparse(substitute()) in function 
# I only see this w/ two or more such substitute() calls; use or don't
#use deparse(), no
# change in this behavior.
# If I do anything w/ ylab (cat it, or create dreck from it) then things
#seem to work
# If I pull the deparsing outside the function definition, it seems to
work -- but then of
# course the user can't specify his own y-label
#seems to be related to my conditional modification of y itself -- if
#comment out
# that line (y+(y==0)*1e-50) everything works. That y->yy seems to fix
#it as well.
badplot<-function(x, y, ylab=deparse(substitute(y)),
xlab=deparse(substitute(x)),  ...)
# un-comment either of the next two lines usually fixes the bug. why?
#	cat(ylab,'\n')
# dreck<-ylab
# creating yy and using it instead of the input y fixes the bug.  why?
#plot(x,yy,xlab=xlab, ylab=ylab, ...)
plot(x,y,xlab=xlab, ylab=ylab, ...)

As the comments say, doing just about any task that involves the input
variable 'y' cleans up the problem.

More information about the R-help mailing list