[R] Evaluation of variable assigned to a function name
Uwe Ligges
ligges at statistik.tu-dortmund.de
Mon Jan 10 15:04:19 CET 2011
On 10.01.2011 14:56, carol white wrote:
> Let the following definitions:
>
>
> # my definition
> my.func<- function (x,y,z){
> ....
> return (v)
> }
>
> # user-defined definition
> my.func<- function (x){
> ...
> return(v)
> }
>
> Considering that my.func can have different parameters but always return a
> vector, how to use v in bar by initializing parameters when calling my.func (x =
> 2 or a = 3,y=4,z=5)? How can my.func could be invoked in bar and v could be
> used?
Well, both versions will need the same number of arguments unless you
want to pass the as well. Example:
bar <- function(arglist, foo){
if(missing(foo)) foo <- my.func
v <- do.call(foo, arglist)
return(v*3)
}
my.func <- function(x,y,z){
return(x+y+z)
}
my.func.user <- function(x){
return(x)
}
Then you can do, e.g.:
bar(arglist=list(x=1, y=2, z=3))
bar(arglist=list(x=1), foo=my.func.user)
Hope this clarifies the idea.
Best,
Uwe
>
> bar<- function(x, ....., foo){
>> if(missing(foo)) foo<- Namespace::my.func
>> .....
>> }
>
>
>
> ----- Original Message ----
> From: Uwe Ligges<ligges at statistik.tu-dortmund.de>
> To: carol white<wht_crl at yahoo.com>
> Cc: r-help at stat.math.ethz.ch
> Sent: Mon, January 10, 2011 2:48:04 PM
> Subject: Re: [R] Evaluation of variable assigned to a function name
>
>
>
> On 10.01.2011 14:39, carol white wrote:
>> In fact, what the function is returning is the most important. So knowing that
>> the parameters and the number of parameters of my.func defined by the user
>> could
>> be different from one definition to another, how to use what my.func returns
> in
>> other functions? Moreover, if the function is defined by the user in an R
>> session, it is then defined globally. In this case, does it need to be passed
>> as
>> a parameter? Note that my.func defined by me is loaded before.
>
> It depends.
>
> 1. The output of a function can always be wrapped in a list.
> 2. You should always pass objects that you want to use in another
> environment unless you really know what you are doing - and reading does
> not suggest you are too sure about it.
> 3. In this case, if your package has a Namespace, your own function
> rather than a user generated one in the .GlobalEnv will be found.
>
> Note that defining stuff in .GlobalEnv and relying on the fact that this
> version is found by another function would imply you really have to get
> it from the specific environment.
>
> Best,
> Uwe
>
>
>
>
>> Carol
>>
>>
>>
>> ----- Original Message ----
>> From: Uwe Ligges<ligges at statistik.tu-dortmund.de>
>> To: carol white<wht_crl at yahoo.com>
>> Cc: r-help at stat.math.ethz.ch
>> Sent: Mon, January 10, 2011 2:11:48 PM
>> Subject: Re: [R] Evaluation of variable assigned to a function name
>>
>> Wel, just let the user give the function in form of an argument, say
>> "foo", and use your code so that
>>
>>
>> bar<- function(x, ....., foo){
>> if(missing(foo)) foo<- Namespace::my.func
>> .....
>> }
>>
>>
>> but perhaps I misunderstood your question.
>>
>> Uwe Ligges
>>
>>
>>
>> On 10.01.2011 13:47, carol white wrote:
>>> Hi,
>>> I have defined a function (my.func) which is used as parameter (f) of another
>>> function. As I want to give the user the possibility to define his own
>> function
>>> instead of my.func, how can I find out if in other functions, the parameter f
>>> has the my.func value (if the user has defined a new function or not)?
>>>
>>>
>>> Moreover, I think I should impose to the user to use another function name
>> than
>>> my.func for this (?). Or a boolean variable is better to be used to indicate
>> if
>>> my function (my.func) or user-defined function is used?
>>>
>>> Thanks
>>>
>>> Carol
>>>
>>> ______________________________________________
>>> 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.
>>
>>
>>
>>
>
>
>
>
More information about the R-help
mailing list