[R] function input as variable name (deparse/quote/paste) ??
Thomas Lumley
tlumley at uw.edu
Sun Mar 11 00:06:16 CET 2012
On Sun, Mar 11, 2012 at 10:29 AM, casperyc <casperyc at hotmail.co.uk> wrote:
> Hi all
>
> Say I have a function:
>
> myname=function(dat,x=5,y=6){
> res<<-x+y-dat
> }
>
> for various input such as
>
> myname(dat1)
> myname(dat2)
> myname(dat3)
> myname(dat4)
> myname(dat5)
>
> how should I modify the 'res' line, to have new informative variable name
> correspondingly, such as
>
> dat1.res
> dat2.res
> dat3.res
> dat4.res
> dat5.res
You *can* do it with
myname=function(dat,x=5,y=6){
name<-paste(deparse(substitute(dat)),"res",sep=".")
assign(name, x+y-dat, parent.frame(), inherits=TRUE)
}
but I would be very surprised if this is actually the best way to do
whatever complex thing you are really doing.
It's very unusual for assignments into the global workspace to be a
useful R programming technique.
-thomas
--
Thomas Lumley
Professor of Biostatistics
University of Auckland
More information about the R-help
mailing list