Function to Define a Function

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Function to Define a Function

Derek Ogle
I am trying to define a general R function that has a function as the output that depends on the user's input arguments (this may make more sense by looking at the toy example below).  My real use for this type of code is to allow a user to choose from many parameterizations of the same general model.

My "issue" is that when I compile a package with this type of code in it I get a __warning__ that "multiple local function definitions for 'm' with different formal arguments."  While this is not a "deadly error" I would like to avoid the warning if possible.  Can someone provide some guidance?  Thank you in advance for any help you can offer.

For what it is worth ... I am working on a Windows XP machine with R 2.11.1.




## A function that allows the user to create a new function that depends on their
##   choice in the type argument.  As a simple example, if the user chooses "one"
##   then the output function is exponential growth, if the user choses "two" then
##   thhe output function is logistic growth.

mdlChooser <- function(type=c("one","two")) {
  type <- match.arg(type)
  switch(type,
    one={ m <- function(x,N0,r) N0*exp(x*r) },
    two={ m <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r)) },
  )
  m
}

## define time steps
t <- 0:10

## create a function -- junk1 -- that produces exponential growth
junk1 <- mdlChooser("one")
junk1
res1 <- junk1(t,500,0.2)
res1

## create a function -- junk2 -- that produces logistic growth
junk2 <- mdlChooser("two")
junk2
res2 <- junk2(t,500,0.2,1000)
res2



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Function to Define a Function

Gabor Grothendieck
On Mon, Aug 9, 2010 at 9:31 PM, Derek Ogle <[hidden email]> wrote:

> I am trying to define a general R function that has a function as the output that depends on the user's input arguments (this may make more sense by looking at the toy example below).  My real use for this type of code is to allow a user to choose from many parameterizations of the same general model.
>
> My "issue" is that when I compile a package with this type of code in it I get a __warning__ that "multiple local function definitions for 'm' with different formal arguments."  While this is not a "deadly error" I would like to avoid the warning if possible.  Can someone provide some guidance?  Thank you in advance for any help you can offer.
>
> For what it is worth ... I am working on a Windows XP machine with R 2.11.1.
>
>
>
>
> ## A function that allows the user to create a new function that depends on their
> ##   choice in the type argument.  As a simple example, if the user chooses "one"
> ##   then the output function is exponential growth, if the user choses "two" then
> ##   thhe output function is logistic growth.
>
> mdlChooser <- function(type=c("one","two")) {
>  type <- match.arg(type)
>  switch(type,
>    one={ m <- function(x,N0,r) N0*exp(x*r) },
>    two={ m <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r)) },
>  )
>  m
> }
>
> ## define time steps
> t <- 0:10
>
> ## create a function -- junk1 -- that produces exponential growth
> junk1 <- mdlChooser("one")
> junk1
> res1 <- junk1(t,500,0.2)
> res1
>
> ## create a function -- junk2 -- that produces logistic growth
> junk2 <- mdlChooser("two")
> junk2
> res2 <- junk2(t,500,0.2,1000)
> res2
>


Try this:

mdlChooser <- function(type = c("one", "two")) {
   one <- function(x,N0,r) N0*exp(x*r)
   two <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r))
   type <- match.arg(type)
   get(type)
}

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Function to Define a Function

Derek Ogle
Gabor ... that worked perfectly.  Thank you.

> -----Original Message-----
> From: Gabor Grothendieck [mailto:[hidden email]]
> Sent: Monday, August 09, 2010 10:20 PM
> To: Derek Ogle
> Cc: R ([hidden email])
> Subject: Re: [R] Function to Define a Function
>
> On Mon, Aug 9, 2010 at 9:31 PM, Derek Ogle <[hidden email]> wrote:
> > I am trying to define a general R function that has a function as the
> output that depends on the user's input arguments (this may make more
> sense by looking at the toy example below).  My real use for this type
> of code is to allow a user to choose from many parameterizations of the
> same general model.
> >
> > My "issue" is that when I compile a package with this type of code in
> it I get a __warning__ that "multiple local function definitions for
> 'm' with different formal arguments."  While this is not a "deadly
> error" I would like to avoid the warning if possible.  Can someone
> provide some guidance?  Thank you in advance for any help you can
> offer.
> >
> > For what it is worth ... I am working on a Windows XP machine with R
> 2.11.1.
> >
> >
> >
> >
> > ## A function that allows the user to create a new function that
> depends on their
> > ##   choice in the type argument.  As a simple example, if the user
> chooses "one"
> > ##   then the output function is exponential growth, if the user
> choses "two" then
> > ##   thhe output function is logistic growth.
> >
> > mdlChooser <- function(type=c("one","two")) {
> >  type <- match.arg(type)
> >  switch(type,
> >    one={ m <- function(x,N0,r) N0*exp(x*r) },
> >    two={ m <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r)) },
> >  )
> >  m
> > }
> >
> > ## define time steps
> > t <- 0:10
> >
> > ## create a function -- junk1 -- that produces exponential growth
> > junk1 <- mdlChooser("one")
> > junk1
> > res1 <- junk1(t,500,0.2)
> > res1
> >
> > ## create a function -- junk2 -- that produces logistic growth
> > junk2 <- mdlChooser("two")
> > junk2
> > res2 <- junk2(t,500,0.2,1000)
> > res2
> >
>
>
> Try this:
>
> mdlChooser <- function(type = c("one", "two")) {
>    one <- function(x,N0,r) N0*exp(x*r)
>    two <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r))
>    type <- match.arg(type)
>    get(type)
> }

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Function to Define a Function

Greg Snow-2
In reply to this post by Derek Ogle
What if you change your function to:

mdlChooser <- function(type=c("one","two")) {
  type <- match.arg(type)
  switch(type,
    one={ function(x,N0,r) N0*exp(x*r) },
    two={ function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r)) },
  )
}

Does that work for you?

--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
[hidden email]
801.408.8111


> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of Derek Ogle
> Sent: Monday, August 09, 2010 7:31 PM
> To: R ([hidden email])
> Subject: [R] Function to Define a Function
>
> I am trying to define a general R function that has a function as the
> output that depends on the user's input arguments (this may make more
> sense by looking at the toy example below).  My real use for this type
> of code is to allow a user to choose from many parameterizations of the
> same general model.
>
> My "issue" is that when I compile a package with this type of code in
> it I get a __warning__ that "multiple local function definitions for
> 'm' with different formal arguments."  While this is not a "deadly
> error" I would like to avoid the warning if possible.  Can someone
> provide some guidance?  Thank you in advance for any help you can
> offer.
>
> For what it is worth ... I am working on a Windows XP machine with R
> 2.11.1.
>
>
>
>
> ## A function that allows the user to create a new function that
> depends on their
> ##   choice in the type argument.  As a simple example, if the user
> chooses "one"
> ##   then the output function is exponential growth, if the user choses
> "two" then
> ##   thhe output function is logistic growth.
>
> mdlChooser <- function(type=c("one","two")) {
>   type <- match.arg(type)
>   switch(type,
>     one={ m <- function(x,N0,r) N0*exp(x*r) },
>     two={ m <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r)) },
>   )
>   m
> }
>
> ## define time steps
> t <- 0:10
>
> ## create a function -- junk1 -- that produces exponential growth
> junk1 <- mdlChooser("one")
> junk1
> res1 <- junk1(t,500,0.2)
> res1
>
> ## create a function -- junk2 -- that produces logistic growth
> junk2 <- mdlChooser("two")
> junk2
> res2 <- junk2(t,500,0.2,1000)
> res2
>
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Function to Define a Function

Thomas Lumley
In reply to this post by Derek Ogle
On Mon, 9 Aug 2010, Derek Ogle wrote:

> I am trying to define a general R function that has a function as the output that depends on the user's input arguments (this may make more sense by looking at the toy example below).  My real use for this type of code is to allow a user to choose from many parameterizations of the same general model.
>
> My "issue" is that when I compile a package with this type of code in it I get a __warning__ that "multiple local function definitions for 'm' with different formal arguments."  While this is not a "deadly error" I would like to avoid the warning if possible.  Can someone provide some guidance?  Thank you in advance for any help you can offer.

<snip>

>
> mdlChooser <- function(type=c("one","two")) {
>  type <- match.arg(type)
>  switch(type,
>    one={ m <- function(x,N0,r) N0*exp(x*r) },
>    two={ m <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r)) },
>  )
>  m
> }
>

One approach is to put the assignment outside the switch

mdlChooser <- function(type=c("one","two")) {
   type <- match.arg(type)
   m<- switch(type,
     one={  function(x,N0,r) N0*exp(x*r) },
     two={ function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r)) },
   )
   m
  }

or not even assign the result

mdlChooser <- function(type=c("one","two")) {
   type <- match.arg(type)
   switch(type,
     one= function(x,N0,r) N0*exp(x*r) ,
     two=function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r)) ,
   )
}

    -thomas

Thomas Lumley
Professor of Biostatistics
University of Washington, Seattle

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Function to Define a Function

Martin Maechler
In reply to this post by Gabor Grothendieck
>>>>> Gabor Grothendieck <[hidden email]>
>>>>>     on Mon, 9 Aug 2010 23:20:18 -0400 writes:

    > On Mon, Aug 9, 2010 at 9:31 PM, Derek Ogle <[hidden email]> wrote:

    >> I am trying to define a general R function that has a
    >> function as the output that depends on the user's input
    >> arguments (this may make more sense by looking at the toy
    >> example below).  My real use for this type of code is to
    >> allow a user to choose from many parameterizations of the
    >> same general model.

    >>
    >> My "issue" is that when I compile a package with this
    >> type of code in it I get a __warning__ that "multiple local
    >> function definitions for 'm' with different formal
    >> arguments."  While this is not a "deadly error" I would like
    >> to avoid the warning if possible.  Can someone provide some
    >> guidance?
    >> Thank you in advance for any help you can offer.
    >>
    >> For what it is worth ... I am working on a Windows XP machine with R 2.11.1.
    >>
    >>

    >> ## A function that allows the user to create a new function that depends on their
    >> ##   choice in the type argument.  As a simple example, if the user chooses "one"
    >> ##   then the output function is exponential growth, if the user choses "two" then
    >> ##   thhe output function is logistic growth.
    >>
    >> mdlChooser <- function(type=c("one","two")) {
    >>  type <- match.arg(type)
    >>  switch(type,
    >>    one={ m <- function(x,N0,r) N0*exp(x*r) },
    >>    two={ m <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r)) },
    >>  )
    >>  m
    >> }
    >>
    >> ## define time steps
    >> t <- 0:10
    >>
    >> ## create a function -- junk1 -- that produces exponential growth
    >> junk1 <- mdlChooser("one")
    >> junk1
    >> res1 <- junk1(t,500,0.2)
    >> res1
    >>
    >> ## create a function -- junk2 -- that produces logistic growth
    >> junk2 <- mdlChooser("two")
    >> junk2
    >> res2 <- junk2(t,500,0.2,1000)
    >> res2
    >>


    > Try this:

    > mdlChooser <- function(type = c("one", "two")) {
    > one <- function(x,N0,r) N0*exp(x*r)
    > two <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r))
    > type <- match.arg(type)
    > get(type)
    > }

or a bit more elegantly, I think,

mdlChooser <- function(type=c("one","two")) {
  switch(match.arg(type),
    one= function(x,N0,r) N0*exp(x*r),
    two= function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r))
  )
}

which just leaves away some unnecessary code from Derek's
original code (and here you could even drop the last "{ .. }"
pair).

Martin Maechler, ETH Zurich

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Function to Define a Function

slre
In reply to this post by Derek Ogle
Neat. But why assign the functions to separate variables at all?

mdlChooser <- function(type=c("one","two")) {
  type <- match.arg(type)
  m <- switch(type,
    one=function(x,N0,r) N0*exp(x*r) ,
    two=function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r))
  )
  m
 }


also works without appearing to assign different functions to the same
variable.
(In this simple example, you wouldn't need the m<- assignment either;
you could simply let the switch return its result. But I assume the real
intended use is more complicated than just returning the function)



>>> Derek Ogle <[hidden email]> 10/08/2010 13:48:13 >>>
Gabor ... that worked perfectly.  Thank you.

> -----Original Message-----
> Try this:
>
> mdlChooser <- function(type = c("one", "two")) {
>    one <- function(x,N0,r) N0*exp(x*r)
>    two <- function(x,N0,r,K) (N0*K)/(N0+(K-N0)*exp(-x*r))
>    type <- match.arg(type)
>    get(type)
> }

______________________________________________
[hidden email] 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.

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}

______________________________________________
[hidden email] 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.