I wanted to add some text to a plot and got (R-1.6.0, Linux):> text(x = c(1, 4), y = 5, labels = x)Error in text.default(x = c(1766, 1895), y = 5, labels = x) : Object "x" not found With the default value of 'labels':> text(x = c(1, 2), y = 5, labels = seq(along = x))Error in seq(along = x) : Object "x" not found A scoping bug? :) But> text(x = c(1, 2), y = 5)is OK. Doesn't give the labels I want, though. But why does it work, i.e., why is 'x' found in this case and not when I write out the default? --- G?ran Brostr?m tel: +46 90 786 5223 Department of Statistics fax: +46 90 786 6614 Ume? University http://www.stat.umu.se/egna/gb/ SE-90187 Ume?, Sweden e-mail: gb at stat.umu.se -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 17 Oct 2002, [iso-8859-1] Göran Broström wrote:> I wanted to add some text to a plot and got (R-1.6.0, Linux): > > > text(x = c(1, 4), y = 5, labels = x) > Error in text.default(x = c(1766, 1895), y = 5, labels = x) : > Object "x" not found > > With the default value of 'labels': > > > text(x = c(1, 2), y = 5, labels = seq(along = x)) > Error in seq(along = x) : Object "x" not found > > A scoping bug? :)No, a scoping misunderstanding. Default arguments are evaluated in the frame of the function: explicit values in the frame of the caller. See `S Programming' p.44, for example.> > But > > > text(x = c(1, 2), y = 5) > > is OK. Doesn't give the labels I want, though. But why does it work, i.e., > why is 'x' found in this case and not when I write out the default? > > --- > Göran Broström tel: +46 90 786 5223 > Department of Statistics fax: +46 90 786 6614 > Umeå University http://www.stat.umu.se/egna/gb/ > SE-90187 Umeå, Sweden e-mail: gb at stat.umu.se > > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 17 Oct 2002, [iso-8859-1] Göran Broström wrote:> > With the default value of 'labels': > > > text(x = c(1, 2), y = 5, labels = seq(along = x)) > Error in seq(along = x) : Object "x" not found > > A scoping bug? :) > > But > > > text(x = c(1, 2), y = 5) > > is OK. Doesn't give the labels I want, though. But why does it work, i.e., > why is 'x' found in this case and not when I write out the default? >Explicit arguments are evaluated in the calling environment and the value is passed to the function. Default arguments are evaluated inside the function, so that defaults like seq(along=x) work. Evaluating explicit arguments inside the function would be a Really Bad Idea, as you would need to know what variables existed inside the function. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._