[R] An argument processing puzzle.

Rolf Turner r.turner at auckland.ac.nz
Mon Jan 18 21:22:32 CET 2010


I have been trying to write a function mv(a,b) to move an
object a to object b.  It returns no value, but only
exists for this side effect.  Basically it just does
b <- a; rm(a).  With some checking and prompting about
over-writing.

The thing is, I'd like to be able to use either call by
name or call by value syntax.  I.e. I want to set up my
argument processing so that

	mv(a,b)
	mv("a",b)
	mv(a,"b")
	mv("a","b")

are all equivalent.  I thought I had achieved this using

     anm <- if(is.character(substitute(a))) a else deparse(substitute 
(a))
     bnm <- if(is.character(substitute(b))) b else deparse(substitute 
(b))

and then working with ``anm'' and ``bnm''.

However the real reason I wanted to be able to use text strings
rather than names as arguments was so that I could do things like

	mv(paste("x",i,sep="."),paste("y",i,sep="."))

(and use such a structure in a for loop, and so forth).

With the paste construction I seem to have to do something like putting
in an ``eval'', as in eval(substitute(a)).  But then the whole thing
falls over when a is a name, i.e. mv(a,b) doesn't work any more.

Is there an incantation that will allow me to accomplish all of my
desiderata?

Thanks.

	cheers,

		Rolf Turner


######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}



More information about the R-help mailing list