[R] substitute() reading data instead of name

From: Carl Witthoft <carl_at_witthoft.com>
Date: Mon, 09 Jun 2008 20:38:29 -0400


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" something?

thanks for your help.
Carl

Here is the function .



# sample of odd behavior when using deparse(substitute()) in function
#call.
# 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?
#yy<-y
#yy<-yy+(yy==0)*1e-50

y<-y+(y==0)*1e-50
#cat(xlab,ylab,'\n')
#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.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 10 Jun 2008 - 00:58:31 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 10 Jun 2008 - 19:30:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive