function input as variable name (deparse/quote/paste) ??

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

function input as variable name (deparse/quote/paste) ??

casperyc
This post was updated on .
Hi all

Say I have a function:

myname=function(dat,x=5,y=6){
    res<<-list()
    res$Model="Model X"
    res$z=x+y
    res$something=x^2+y^2
    return(res)
}

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

stored in the workspace.

This is only an example of a complex function I have written.

Thanks in advance!

Casper


######################
PhD candidate in Statistics
Big R Fan
Big LEGO Fan
Big sTaTs Fan
######################
Reply | Threaded
Open this post in threaded view
|

Re: function input as variable name (deparse/quote/paste) ??

Hans Ekbrand
On Sat, Mar 10, 2012 at 01:29:16PM -0800, casperyc 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
>
> stored in the workspace.

Why not keep the information of input values in a list, or vector?
What is gained by storing that info in the variable _name_ ? Your
function could return a list with both the result and the input value.

While you did say that this was part of something complex, I suspect
your post might be a case of "Being overly specific and not stating
your real goal."

--
Hans Ekbrand (http://sociologi.cjb.net) <[hidden email]>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: function input as variable name (deparse/quote/paste) ??

Thomas Lumley-2
In reply to this post by casperyc
On Sun, Mar 11, 2012 at 10:29 AM, casperyc <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: function input as variable name (deparse/quote/paste) ??

casperyc
In reply to this post by Hans Ekbrand
Sorry if I wasn't stating what I really wanted or it was a bit confusing.

Basically, there are MANY datasets to run suing the same function

I have written a function to analyze it and returns a LIST of useful out put in the variable 'res' (to the workspace).

I also created another script run.r such as

myname(dat1)
myname(dat2)
myname(dat3)
myname(dat4)
myname(dat5)

For now, each time the output in the main workspace 'res' (the list) is over written.

I want it to have different suffix to differentiate them. So I can have a look later after the batch is run.

Thanks.

casper
######################
PhD candidate in Statistics
Big R Fan
Big LEGO Fan
Big sTaTs Fan
######################
Reply | Threaded
Open this post in threaded view
|

Re: function input as variable name (deparse/quote/paste) ??

Berend Hasselman

On 11-03-2012, at 01:01, casperyc wrote:

> Sorry if I wasn't stating what I really wanted or it was a bit confusing.
>
> Basically, there are MANY datasets to run suing the same function
>
> I have written a function to analyze it and returns a LIST of useful out put
> in the variable 'res' (to the workspace).
>

Your function uses return?
Probably not.

> I also created another script run.r such as
>
> myname(dat1)
> myname(dat2)
> myname(dat3)
> myname(dat4)
> myname(dat5)
>
> For now, each time the output in the main workspace 'res' (the list) is over
> written.
>
> I want it to have different suffix to differentiate them. So I can have a
> look later after the batch is run.

Well, if that is the case then there is a better way than doing global assignments in a function.

Make sure myfunction returns the list of results with return() and don't do global assignment with <<-

for( k in 1:5) {
        dataname <- paste("data",k,sep="")
        resname   <- paste("res",k,sep="")
        assign(resname, myfunction(get(dataname)))
}


Berend

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: function input as variable name (deparse/quote/paste) ??

Hans Ekbrand
In reply to this post by casperyc
On Sat, Mar 10, 2012 at 04:01:21PM -0800, casperyc wrote:

> Sorry if I wasn't stating what I really wanted or it was a bit confusing.
>
> Basically, there are MANY datasets to run suing the same function
>
> I have written a function to analyze it and returns a LIST of useful out put
> in the variable 'res' (to the workspace).
>
> I also created another script run.r such as
>
> myname(dat1)
> myname(dat2)
> myname(dat3)
> myname(dat4)
> myname(dat5)
>
> For now, each time the output in the main workspace 'res' (the list) is over
> written.
>
> I want it to have different suffix to differentiate them. So I can have a
> look later after the batch is run.

I see no advantage in having that information in variable names. Just

- add the name of the data set to the information that is included in
  the returned list.

- run your function with sapply() and the returned list of sapply will
  be a list of lists.

--
Hans Ekbrand (http://sociologi.cjb.net) <[hidden email]>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: function input as variable name (deparse/quote/paste) ??

casperyc
In reply to this post by casperyc
Thank you everyone for your reply.

Like I said in my original post, this is just a demonstrative example of my 'big' self written script.

My 'big' function take several inputs, of which the first 1 is the dataset and returns a LIST variable 'res<<-list()' to the workspace with many information.

The names of my actual datasets are NOT in any pattern, like 'dat1', 'dat2', 'dat3'. That's why i wonder if I can modify
the line 'res<<-' in anyway to be 'res.dat<<-' where 'dat' in the first input. So I can CALL via 'res.dat' (or res.newdata, res.olddata, res.tmpdata,res.hisdata) in the workspace any time I want to have a look.
######################
PhD candidate in Statistics
Big R Fan
Big LEGO Fan
Big sTaTs Fan
######################