Re: [R] from function to its name?
On Mar 2, 2007, at 9:42 AM, Ido M. Tamir wrote: Hi, I can get from a string to a function with this name: f1 - function(x){ mean(x) } do.call(f1,list{1:4}) get(f1) etc... But how do I get from a function to its name? funcVec - c(f1,median) funcVec [[1]] function(x){ mean(x) } I suppose you could do: funcVec but that's probably not what you want ;). Can you do this with any object in R? In what situation will you be wanting this name? I mean, how would you be given this object, but not know its name in advance? If it is passed as an argument in a function or something, then what would you consider to be its name? I.e. I don't really see where you would reasonably want to do something like this, without there being another way around it. Btw, perhaps this does what you want: as.character(quote(f)) Haris Skiadas Department of Mathematics and Computer Science Hanover College __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] from function to its name?
On Fri, Mar 02, 2007 at 03:42:46PM +0100, Ido M. Tamir wrote: Hi, I can get from a string to a function with this name: f1 - function(x){ mean(x) } do.call(f1,list{1:4}) get(f1) etc... But how do I get from a function to its name? funcVec - c(f1,median) funcVec [[1]] function(x){ mean(x) } str(funcVec) List of 2 $ :function (x) ..- attr(*, source)= chr function(x){ mean(x) } $ :function (x, ...) deparse(funcVec[1]) [1] list(function (x) { mean(x) [4] }) for any symbol/name deparse(substitute(f1)) yields the string representation, but this won't give you f1 for deparse(substitute(funcVec[1])). but rather the string funcVec[1]. if you actually want to access funcVec components via strings denoting the components, maybe you simply could use funcVec - list(f1 = f1, median = median) and access these via funcVec[[f1]] which would enable using a string variable holding the name: dum = f1 funcVec[[dum]] hth joerg __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] from function to its name?
Charilaos Skiadas wrote: On Mar 2, 2007, at 9:42 AM, Ido M. Tamir wrote: But how do I get from a function to its name? Can you do this with any object in R? In what situation will you be wanting this name? I mean, how would you be given this object, but not know its name in advance? If it is passed as an argument in a function or something, then what would you consider to be its name? I.e. I don't really see where you would reasonably want to do something like this, without there being another way around it. I wanted to pass a vector of functions as an argument to a function to do some calculations and put the results in a list where each list entry has the name of the function. I thought I could either pass a vector of function names as character, then retrieve the functions etc... Or do the opposite, pass the functions and then retrieve the names, but this seems not to be possible it occurred to me, hence my question. thanks ido __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] from function to its name?
Ido M. Tamir [EMAIL PROTECTED] writes: I wanted to pass a vector of functions as an argument to a function to do some calculations and put the results in a list where each list entry has the name of the function. I thought I could either pass a vector of function names as character, then retrieve the functions etc... Or do the opposite, pass the functions and then retrieve the names, but this seems not to be possible it occurred to me, hence my question. Functions don't have to have names, by which I mean that the definition doesn't have to be bound to a symbol. If your function takes a list of functions then: yourFunc(theFuncs=list(function(x) x + 1)) You could force the list to have names and use them. Or you could force function names to be passed in (your other idea). + seth __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] from function to its name?
Seth is, of course, correct, but perhaps the following may help: ## function that takes a function as an argument foo - function(f,x)list(deparse(substitute(f)),f(x)) ## Value is a list of length 2; first component is a character string giving the name of the funtion; second component is the result of applying the function to the x argument. ##pass in the name (UNquoted) of the function as the first argument ## This works because the evaluator looks up the function that the symbol is bound to in the usual way foo(mean, 1:5) [[1]] [1] mean [[2]] [1] 3 ## pass in an unnamed function as the first argument foo(function(y)sum(y)/length(y), 1:5) [[1]] [1] function(y) sum(y)/length(y) [[2]] [1] 3 ## the following gives an error since the first argument is a character string, not a name/symbol: foo(f=mean, 1:5) Error in foo(f = mean, 1:5) : could not find function f Cheers, Bert Gunter Genentech Nonclinical Statistics South San Francisco, CA 94404 650-467-7374 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Seth Falcon Sent: Friday, March 02, 2007 9:18 AM To: r-help@stat.math.ethz.ch Subject: Re: [R] from function to its name? Ido M. Tamir [EMAIL PROTECTED] writes: I wanted to pass a vector of functions as an argument to a function to do some calculations and put the results in a list where each list entry has the name of the function. I thought I could either pass a vector of function names as character, then retrieve the functions etc... Or do the opposite, pass the functions and then retrieve the names, but this seems not to be possible it occurred to me, hence my question. Functions don't have to have names, by which I mean that the definition doesn't have to be bound to a symbol. If your function takes a list of functions then: yourFunc(theFuncs=list(function(x) x + 1)) You could force the list to have names and use them. Or you could force function names to be passed in (your other idea). + seth __ R-help@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 and provide commented, minimal, self-contained, reproducible code. __ R-help@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 and provide commented, minimal, self-contained, reproducible code.
Re: [R] from function to its name?
Check out: https://stat.ethz.ch/pipermail/r-help/2006-October/114431.html On 3/2/07, Ido M. Tamir [EMAIL PROTECTED] wrote: Charilaos Skiadas wrote: On Mar 2, 2007, at 9:42 AM, Ido M. Tamir wrote: But how do I get from a function to its name? Can you do this with any object in R? In what situation will you be wanting this name? I mean, how would you be given this object, but not know its name in advance? If it is passed as an argument in a function or something, then what would you consider to be its name? I.e. I don't really see where you would reasonably want to do something like this, without there being another way around it. I wanted to pass a vector of functions as an argument to a function to do some calculations and put the results in a list where each list entry has the name of the function. I thought I could either pass a vector of function names as character, then retrieve the functions etc... Or do the opposite, pass the functions and then retrieve the names, but this seems not to be possible it occurred to me, hence my question. thanks ido __ R-help@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 and provide commented, minimal, self-contained, reproducible code. __ R-help@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 and provide commented, minimal, self-contained, reproducible code.