[R] Function assignment

peter dalgaard pdalgd at gmail.com
Sat Jul 26 22:03:54 CEST 2014


On 26 Jul 2014, at 17:01 , Jeff Newmiller <jdnewmil at dcn.davis.ca.us> wrote:

> What an awful idea... that would lead to incredibly hard-to-debug programs. No, you cannot do that. What kind of problem has led you to want such a capability? Perhaps we can suggest a simpler way to think about your problem.

I agree that this is a silly idea, but I actually thought that it could be done by clever manipulation of the call stack. It can if you do the assignment with assign():

> foo <- function()sys.calls()[[1]][[2]]
> assign("z", foo())
> z
[1] "z"
> assign("bah", foo())
> bah
[1] "bah"

but if you do x <- foo(), there is no mention of x or "x" in sys.calls().

Anyways, functions that assume being called in a specific are asking for trouble in all cases where they get called differently.

-pd



> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
>                                      Live:   OO#.. Dead: OO#..  Playing
> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
> /Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
> --------------------------------------------------------------------------- 
> Sent from my phone. Please excuse my brevity.
> 
> On July 26, 2014 5:29:59 AM PDT, Florian Ryan <florian.ryan at aim.com> wrote:
>> Hello,
>> 
>> I would like to use the variable name which i assign the return value
>> of a function in a function. Is that possible?
>> e.g.
>> 
>> foo <- function(){
>>   some not to me known R magic
>> }
>> 
>> myVariableName <- foo()
>> myVariableName
>> [1] "myVariableName"
>> 
>> Hope someone can help me.
>> 
>> Thanks
>> Florian
>> 
>> 	[[alternative HTML version deleted]]
>> 
>> ______________________________________________
>> R-help at r-project.org 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.
> 
> ______________________________________________
> R-help at r-project.org 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list