Ales Ziberna
2006-Jan-04 14:14 UTC
[R] Putting an object in to a function that calls the current function
Hello! I would like to put an object in to a function that calls the current function. I thought the answer will be clear to me after reading the help files: ?assign ?sys.parent However it is not. Here is an example I thought should work, however it dose not exactly: f<-function(){s();print(a)} s<-function()assign(x="a",value="ok",pos=sys.parent()) f() #I want to get "ok" a #I do not want "a" in global enviorment, so here I should get #Error: Object "a" not found ff<-function()f() #here I also want to get "ok" - it should not matter if the parent fuction has any parents Thank you in advance for suggestions! Ales Ziberna
Gabor Grothendieck
2006-Jan-04 14:21 UTC
[R] Putting an object in to a function that calls the current function
Check out: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/64929.html On 1/4/06, Ales Ziberna <aleszib at gmail.com> wrote:> Hello! > > I would like to put an object in to a function that calls the current > function. > > I thought the answer will be clear to me after reading the help files: > ?assign > ?sys.parent > > However it is not. > Here is an example I thought should work, however it dose not exactly: > > f<-function(){s();print(a)} > s<-function()assign(x="a",value="ok",pos=sys.parent()) > f() #I want to get "ok" > a #I do not want "a" in global enviorment, so here I should get > #Error: Object "a" not found > ff<-function()f() #here I also want to get "ok" - it should not matter if > the parent fuction has any parents > > Thank you in advance for suggestions! > > Ales Ziberna > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
Duncan Murdoch
2006-Jan-04 14:26 UTC
[R] Putting an object in to a function that calls the current function
On 1/4/2006 9:14 AM, Ales Ziberna wrote:> Hello! > > I would like to put an object in to a function that calls the current > function. > > I thought the answer will be clear to me after reading the help files: > ?assign > ?sys.parent > > However it is not. > Here is an example I thought should work, however it dose not exactly: > > f<-function(){s();print(a)} > s<-function()assign(x="a",value="ok",pos=sys.parent()) > f() #I want to get "ok" > a #I do not want "a" in global enviorment, so here I should get > #Error: Object "a" not found > ff<-function()f() #here I also want to get "ok" - it should not matter if > the parent fuction has any parents > > Thank you in advance for suggestions!That's not a good idea. Why would you want to do something like that? That out of the way, here's a function that does it: f<-function(){s();print(a)} s<-function()assign(x="a",value="ok",env=parent.frame()) The difference between pos=sys.parent() and env=parent.frame() is that the pos is interpreted as a position in the search list (see ?assign), while parent.frame() gives you the environment from the stack, equivalent to sys.frame(sys.parent()). In R you're almost certainly better off working directly with environments, rather than going through integer indexing the way you (used to?) have to do in S-PLUS. Did I mention that messing with the environment of your caller is a bad idea? It's not yours, don't touch it. Duncan Murdoch