[R] Superassignment (<<-) and indexing

Spencer Graves spencer.graves at pdf.com
Fri Sep 2 23:43:41 CEST 2005


	  Permit a mild protest on the word "appropriate" in this context.  The 
global assignment operator "<<-" provides, for my tastes, excessive 
opportunities for problems.  If I define "x" someplace else and then 
call your function, it may change my "x" in ways that generate 
considerable wailing and gnashing of teeth.  Unless I assign the 
function output to "x", then the action of your function will change my 
"x" in ways I did not anticipate, possibly generating many problems for 
me later -- with extreme difficulties in finding the source of the 
problem.  Moreover, if your library expects to later find in "x" what 
your function stored there, there could be other problems, because I 
might redefine "x" before you use it.  The library might work fine when 
you use it but not for someone else -- and tracing the problem can be 
difficult.

	  I understand that "<<-" may allow your function f1 to call f2 and 
have f2 change "x" in f1.  However, if your f2 gets called some other 
way or if the name of "x" is misspelled or changed in either f1 or f2, 
we could be back to the situation I just described.

	  spencer graves

Brahm, David wrote:

> In a clean environment under R-2.1.0 on Linux:
> 
>>x <- 1:5
>>x[3] <<- 9
> 
> Error: Object "x" not found
> 
> Isn't that odd?  (Note x <<- 9 works just fine.)
> 
> Why am I doing this?  Because I'm stepping through code that
> normally lives inside a function, where "<<-" is appropriate.
> 
> -- David Brahm (brahm at alum.mit.edu)
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

-- 
Spencer Graves, PhD
Senior Development Engineer
PDF Solutions, Inc.
333 West San Carlos Street Suite 700
San Jose, CA 95110, USA

spencer.graves at pdf.com
www.pdf.com <http://www.pdf.com>
Tel:  408-938-4420
Fax: 408-280-7915




More information about the R-help mailing list