Skip to content

Evaluation of variable assigned to a function name

12 messages · Uwe Ligges, carol white, Tóth Dénes

#
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
#
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:
#
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.

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:
function
than
if
#
On 10.01.2011 14:39, carol white wrote:
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
#
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?

bar<- function(x, ....., foo){
----- 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
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
#
On 10.01.2011 14:56, carol white wrote:
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
#
Note that I will call my function or user-defined function in bar function to 
have the value that my or user-defined function returns. So how can these 
function be invoked? Can it be like this?

bar <- function(arglist, foo){
     if(missing(foo)) {
           foo <- my.func
         v = my.func(x = 2)
     }
    else{
         foo <- my.func.user
         v <- do.call(foo, arglist) # since arglist is not known in advance
    }
     return(v*3)
}



----- 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 3:04:19 PM
Subject: Re: [R] Evaluation of variable assigned to a function name
On 10.01.2011 14:56, carol white wrote:
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
that
another
f
#
On 10.01.2011 16:41, carol white wrote:
The line abopve does nt make sense now, the rest seems to be fine.

Uwe
#
If it doesn't make sense, how to invoke in the bar function, my.func with 
specified parameters with known values and how to invoke my.func.user if the 
user defines his own function (without specifying the parameters)?



----- 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 6:10:58 PM
Subject: Re: [R] Evaluation of variable assigned to a function name
On 10.01.2011 16:41, carol white wrote:
The line abopve does nt make sense now, the rest seems to be fine.

Uwe
(x
passed
indicate
http://www.R-project.org/posting-guide.html
#
On 10.01.2011 18:24, carol white wrote:
he arguments must be specified in arglist, of course.

I guess you want something completely different, hence it might make 
sense to explain what you really intend to do and what your code is so far.

Uwe Ligges
#
I want to make an abstraction of the parameters (assuming that they are unknown) 
for the user-defined function. Is it possible?

In the bar function, if the user doesn't define any function, my.func will be 
invoked with known parameters (if part of the code). If the user defines his own 
function, his function will be invoked in the bar function instead of my.func 
(the else part of the code). The value of either function (my.func or 
my.func.user) will be returned and used in the rest of the bar function.

Is there any thing else that should be clarified?



----- 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 7:14:00 PM
Subject: Re: [R] Evaluation of variable assigned to a function name
On 10.01.2011 18:24, carol white wrote:
he arguments must be specified in arglist, of course.

I guess you want something completely different, hence it might make 
sense to explain what you really intend to do and what your code is so far.

Uwe Ligges
returns
parameter
#
Dear Experts,

We conducted an experiment with 2 within-subject factors. We have very
good reason to use gamlss, which works fine for our dataset, but
unfortunately in the final model the vcov matrix can not be produced. This
possibility is documented in the gamlss manual, but no hint is given how
to resolve the problem if multiple comparisons are needed. (Actually I
prefer building models with and without the given term and check the
information criteria or running likelihood-ratio tests, but reviewers on
my field do insist on such direct comparisons.)

Here I give you a short example (the actual design is far more
complicated, but the problem is essentially the same):

#############################
# EXAMPLE
#############################

library(gamlss)

# make it reproducable
set.seed(1234)

# define the subject factor and two within-subject factors
# (fa & fb, with 2 and 3 levels, respectively)
datfr <- expand.grid(subj=1:20,fa=0:1,fb=0:2)[rep(1:120,5),]

# define dependent variable
datfr$y <- with(datfr,
    rep(sample(rnorm(20,0,2)),30) +
    fa*2+ifelse(fa==1,rnorm(600,0,2),rnorm(600,0,1)) +
    fb*3+rnorm(600,0,fb) +
    fa*fb*3)
datfr$fa <- factor(datfr$fa)
datfr$fb <- factor(datfr$fb)
datfr$subj <- factor(datfr$subj)

# our dataset is unbalanced, that's why
datfr <- datfr[sample(600,400),]

# gamlss model
m.gamlss <- gamlss(y~ fa*fb + random(subj),
     sigma.fo= ~fa +fb, data=datfr)

summary(m.gamlss)

###############################



Regards,
  Denes