Casper Ti. Vector
2012-May-03 10:31 UTC
[R] `mapply(function(x) function() x, c("a", "b"))$a()' returns `"b"'
As the title says, I want to apply a function (which itself returns a function) to a list (or vector), and get a list (or vector) of generated functions as the return value, but get unexpected result. Anyone with an idea about the reason of this phenomenon and a correct way to implement the requirements? Thanks very much :) -- Using GPG/PGP? Please get my current public key (ID: 0xAEF6A134, valid from 2010 to 2013) from a key server. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: Digital signature URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120503/bc0b3e2a/attachment.bin>
Jessica Streicher
2012-May-03 13:39 UTC
[R] `mapply(function(x) function() x, c("a", "b"))$a()' returns `"b"'
As i see it you will save the actual "text" of the function - and when you call it later on it takes the last value of x it has encountered as the value. I guess you want the x not to be saved as x, but as "a" or "b", so, as its value. I am not sure how to do that however as of yet. Am 03.05.2012 um 12:31 schrieb Casper Ti. Vector:> As the title says, I want to apply a function (which itself returns > a function) to a list (or vector), and get a list (or vector) of > generated functions as the return value, but get unexpected result. > > Anyone with an idea about the reason of this phenomenon and a correct > way to implement the requirements? Thanks very much :) > > -- > Using GPG/PGP? Please get my current public key (ID: 0xAEF6A134, valid > from 2010 to 2013) from a key server. > > ______________________________________________ > 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.
Jessica Streicher
2012-May-03 14:02 UTC
[R] `mapply(function(x) function() x, c("a", "b"))$a()' returns `"b"'
Now.. i just tried around and this might be a bit strange way to do things.. createFunc<-function(v){ v_out<-NULL for(i in v){ v_out[[i]]<-substitute(function(){x},list(x=i)) } return(v_out) }> y<-createFunc(c("a","b")) > y$a function() { "a" } $b function() { "b" }> eval(y$a)()[1] "a"> eval(y$b)()[1] "b" Am 03.05.2012 um 12:31 schrieb Casper Ti. Vector:> As the title says, I want to apply a function (which itself returns > a function) to a list (or vector), and get a list (or vector) of > generated functions as the return value, but get unexpected result. > > Anyone with an idea about the reason of this phenomenon and a correct > way to implement the requirements? Thanks very much :) > > -- > Using GPG/PGP? Please get my current public key (ID: 0xAEF6A134, valid > from 2010 to 2013) from a key server. > > ______________________________________________ > R-help@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.[[alternative HTML version deleted]]
Maybe Matching Threads
- ggplot2: expression() in legend labels?
- ggplot2: behaviour with empty datasets
- Problem with logarithmic nonlinear model using nls() from the `stats' package
- [LLVMdev] EXC_BAD_ACCESS: invalid MemoryBuffer from ContentCache::getBuffer
- [PATCH 0/3] OpenUPS updates