Re: [R] Determining name of calling function.
Not answering your question, but just pointing out the example of base::.NotYetImplemented() essentially doing the same thing. Best, baptiste 2009/9/28 Rolf Turner r.tur...@auckland.ac.nz: I have vague recollections of seeing this question discussed on r-help previously, but I can't find the relevant postings. I want to determine (from within a given function) the name of the function calling that given function. E.g. if I have a function foo() which calls a function bar(), and also a function clyde() which calls bar(), I want to have, in the code of bar(), an instruction which will return the character string foo if bar() was called from foo() and the string clyde if bar() was called from clyde(). Without really understanding what I'm doing I cobbled together the following: fname - as.character(sys.call(-1))[1] This ***seems*** to work, at least in simple test cases. But is it reliably robust? Are there traps for young players that I am not seeing? My ``solution'' returns NA as the value of fname if bar() is called from the command line, rather than being called by foo() or clyde(). This is acceptable. I think Any avuncular advice from those younger and wiser than myself? :-) cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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. __ 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.
Re: [R] Determining name of calling function.
baptiste auguie wrote: Not answering your question, but just pointing out the example of base::.NotYetImplemented() essentially doing the same thing. Best, baptiste 2009/9/28 Rolf Turner r.tur...@auckland.ac.nz: I have vague recollections of seeing this question discussed on r-help previously, but I can't find the relevant postings. I want to determine (from within a given function) the name of the function calling that given function. E.g. if I have a function foo() which calls a function bar(), and also a function clyde() which calls bar(), I want to have, in the code of bar(), an instruction which will return the character string foo if bar() was called from foo() and the string clyde if bar() was called from clyde(). Without really understanding what I'm doing I cobbled together the following: fname - as.character(sys.call(-1))[1] This ***seems*** to work, at least in simple test cases. But is it reliably robust? Are there traps for young players that I am not seeing? My ``solution'' returns NA as the value of fname if bar() is called from the command line, rather than being called by foo() or clyde(). This is acceptable. I think Any avuncular advice from those younger and wiser than myself? :-) cheers, Rolf Turner (How old are you? Surely you are aware that avuncular advice usually comes from your parents' siblings and their spouses.) I'd maybe be more inclined to use something like deparse(sys.call(-1)[[1]]) but there's no clear benefit. The main trap would be that the first element of the call is not necessarily a name. Consider (function(x)deparse(sys.call()[[1]]))(x=2) [1] (function(x) deparse(sys.call()[[1]])) (function(x)as.character(sys.call()[1]))(x=2) [1] (function(x) as.character(sys.call()[1])) and notice that in fact the function bing called may not even _have_ a name. -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 __ 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.
Re: [R] Determining name of calling function.
(Oops, that was of course intended for Rolf, not Baptiste) Peter Dalgaard wrote: baptiste auguie wrote: Not answering your question, but just pointing out the example of base::.NotYetImplemented() essentially doing the same thing. Best, baptiste 2009/9/28 Rolf Turner r.tur...@auckland.ac.nz: I have vague recollections of seeing this question discussed on r-help previously, but I can't find the relevant postings. I want to determine (from within a given function) the name of the function calling that given function. E.g. if I have a function foo() which calls a function bar(), and also a function clyde() which calls bar(), I want to have, in the code of bar(), an instruction which will return the character string foo if bar() was called from foo() and the string clyde if bar() was called from clyde(). Without really understanding what I'm doing I cobbled together the following: fname - as.character(sys.call(-1))[1] This ***seems*** to work, at least in simple test cases. But is it reliably robust? Are there traps for young players that I am not seeing? My ``solution'' returns NA as the value of fname if bar() is called from the command line, rather than being called by foo() or clyde(). This is acceptable. I think Any avuncular advice from those younger and wiser than myself? :-) cheers, Rolf Turner (How old are you? Surely you are aware that avuncular advice usually comes from your parents' siblings and their spouses.) I'd maybe be more inclined to use something like deparse(sys.call(-1)[[1]]) but there's no clear benefit. The main trap would be that the first element of the call is not necessarily a name. Consider (function(x)deparse(sys.call()[[1]]))(x=2) [1] (function(x) deparse(sys.call()[[1]])) (function(x)as.character(sys.call()[1]))(x=2) [1] (function(x) as.character(sys.call()[1])) and notice that in fact the function bing called may not even _have_ a name. -- O__ Peter Dalgaard Øster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - (p.dalga...@biostat.ku.dk) FAX: (+45) 35327907 __ 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.
[R] Determining name of calling function.
I have vague recollections of seeing this question discussed on r-help previously, but I can't find the relevant postings. I want to determine (from within a given function) the name of the function calling that given function. E.g. if I have a function foo() which calls a function bar(), and also a function clyde() which calls bar(), I want to have, in the code of bar(), an instruction which will return the character string foo if bar() was called from foo() and the string clyde if bar() was called from clyde(). Without really understanding what I'm doing I cobbled together the following: fname - as.character(sys.call(-1))[1] This ***seems*** to work, at least in simple test cases. But is it reliably robust? Are there traps for young players that I am not seeing? My ``solution'' returns NA as the value of fname if bar() is called from the command line, rather than being called by foo() or clyde(). This is acceptable. I think Any avuncular advice from those younger and wiser than myself? :-) cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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.
Re: [R] Determining name of calling function.
Not sure if this is important to you but R functions don't have to have names so what you get back won't be a name if the function was anonymous. In the example below an anonymous function calls fname and the returned string is the calling sequence but that's not its name since it has no name. In fact, in a sense no R functions have names. You can store them in variables and call that variable its name but that is not an intrinsic part of the function itself. A function is just an environment, an argument list and a body -- no name. fname - function() as.character(sys.call(-1))[1] (function() fname())() [1] (function() fname()) On Sun, Sep 27, 2009 at 7:24 PM, Rolf Turner r.tur...@auckland.ac.nz wrote: I have vague recollections of seeing this question discussed on r-help previously, but I can't find the relevant postings. I want to determine (from within a given function) the name of the function calling that given function. E.g. if I have a function foo() which calls a function bar(), and also a function clyde() which calls bar(), I want to have, in the code of bar(), an instruction which will return the character string foo if bar() was called from foo() and the string clyde if bar() was called from clyde(). Without really understanding what I'm doing I cobbled together the following: fname - as.character(sys.call(-1))[1] This ***seems*** to work, at least in simple test cases. But is it reliably robust? Are there traps for young players that I am not seeing? My ``solution'' returns NA as the value of fname if bar() is called from the command line, rather than being called by foo() or clyde(). This is acceptable. I think Any avuncular advice from those younger and wiser than myself? :-) cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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. __ 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.
Re: [R] Determining name of calling function.
On 28/09/2009, at 12:34 PM, Gabor Grothendieck wrote: Not sure if this is important to you but R functions don't have to have names so what you get back won't be a name if the function was anonymous. In the example below an anonymous function calls fname and the returned string is the calling sequence but that's not its name since it has no name. In fact, in a sense no R functions have names. You can store them in variables and call that variable its name but that is not an intrinsic part of the function itself. A function is just an environment, an argument list and a body -- no name. fname - function() as.character(sys.call(-1))[1] (function() fname())() [1] (function() fname()) snip Good point. Thanks. I don't ***think*** that this issue will call problems for me. In my real application ``bar()'' will always be called by a named function. Still, it's something to keep in mind. cheers, Rolf Turner ## Attention:\ This e-mail message is privileged and confid...{{dropped:9}} __ 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.