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]]
On Mon, Aug 9, 2010 at 9:31 PM, Derek Ogle <DOgle at northland.edu> 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) }
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 greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Derek Ogle > Sent: Monday, August 09, 2010 7:31 PM > To: R (r-help at R-project.org) > 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]] > > ______________________________________________ > 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.
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
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 <DOgle at northland.edu> 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) > }______________________________________________ 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. ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}