Hi, Thanks for your suggestion. missing(x) works only if x is not altered within the function, which is not the case in my actual function (which is more complex than the example function I send in my previous post). Are you aware of any function what would query the original function call arguments no matter what happens in the function? Sebastien On 3/24/2016 8:47 AM, Jeff Newmiller wrote:> Don't provide a default value for c in the parameter list. Then you > can use the missing() function to make decisions, including whether to > generate a default value for c or not. > -- > Sent from my phone. Please excuse my brevity. > > On March 24, 2016 5:11:43 AM PDT, sbihorel > <Sebastien.Bihorel at cognigencorp.com> wrote: > > Hi, > > Please consider the following functions: > > myf1 <- function(x,fun){ > if (is.null(fun)){ > x > } else { > do.call(fun, list(x)) > } > } > > myf2 <- function(a=1, b=2, fun=NULL, c=myfun1(b,fun)){ > > if (myf1(b,fun)>0 & <c was not provided in function call>){ > c <- b > } > print(list(a,b,c)) > } > > myf2(a=2,b=3,fun=exp) > myf2(a=2,b=3,fun=exp,c=4) > > I need to replace "<c was not provided in function call>" by some code > that would make myf2 aware of it own calls and of the fact that the c > argument was provided or not in the function calls. > > Thanks for your help > > Sebastien > > ------------------------------------------------------------------------ > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE d > o read > the posting guidehttp://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Sebastien Bihorel Associate Director, Pharmacometrics Buffalo Office: +1-716-633-3463 ext. 323 | Website <http://www.cognigencorp.com> <http://www.simulations-plus.com/Default.aspx>
Duncan Murdoch
2016-Mar-24 14:40 UTC
[R] How to make a function aware of its own call arguments
On 24/03/2016 10:28 AM, sbihorel wrote:> Hi, > > Thanks for your suggestion. > > missing(x) works only if x is not altered within the function, which is > not the case in my actual function (which is more complex than the > example function I send in my previous post).That's not quite true: missing(x) works *before* x is altered within the function. It works even if x has a default value. So your myf2 could just use missing(c): myf2 <- function(a=1, b=2, fun=NULL, c=myfun1(b,fun)){ if (myf1(b,fun)>0 && missing(c)){ c <- b } print(list(a,b,c)) }> > Are you aware of any function what would query the original function > call arguments no matter what happens in the function?The match.call() function does that, but I think you don't need that: f <- function(x = 1, ...) { x <- 2 match.call() } f(3) ## Prints: f(x = 3) Duncan Murdoch> > Sebastien > > On 3/24/2016 8:47 AM, Jeff Newmiller wrote: > > Don't provide a default value for c in the parameter list. Then you > > can use the missing() function to make decisions, including whether to > > generate a default value for c or not. > > -- > > Sent from my phone. Please excuse my brevity. > > > > On March 24, 2016 5:11:43 AM PDT, sbihorel > > <Sebastien.Bihorel at cognigencorp.com> wrote: > > > > Hi, > > > > Please consider the following functions: > > > > myf1 <- function(x,fun){ > > if (is.null(fun)){ > > x > > } else { > > do.call(fun, list(x)) > > } > > } > > > > myf2 <- function(a=1, b=2, fun=NULL, c=myfun1(b,fun)){ > > > > if (myf1(b,fun)>0 & <c was not provided in function call>){ > > c <- b > > } > > print(list(a,b,c)) > > } > > > > myf2(a=2,b=3,fun=exp) > > myf2(a=2,b=3,fun=exp,c=4) > > > > I need to replace "<c was not provided in function call>" by some code > > that would make myf2 aware of it own calls and of the fact that the c > > argument was provided or not in the function calls. > > > > Thanks for your help > > > > Sebastien > > > > ------------------------------------------------------------------------ > > > > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE d > > o read > > the posting guidehttp://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > -- Sebastien Bihorel Associate Director, Pharmacometrics Buffalo Office: > +1-716-633-3463 ext. 323 | Website <http://www.cognigencorp.com> > <http://www.simulations-plus.com/Default.aspx> > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Thanks for the precision. On 3/24/2016 10:40 AM, Duncan Murdoch wrote:> On 24/03/2016 10:28 AM, sbihorel wrote: >> Hi, >> >> Thanks for your suggestion. >> >> missing(x) works only if x is not altered within the function, which is >> not the case in my actual function (which is more complex than the >> example function I send in my previous post). > > That's not quite true: missing(x) works *before* x is altered within > the function. It works even if x has a default value. > > So your myf2 could just use missing(c): > > myf2 <- function(a=1, b=2, fun=NULL, c=myfun1(b,fun)){ > > if (myf1(b,fun)>0 && missing(c)){ > c <- b > } > print(list(a,b,c)) > } > > >> >> Are you aware of any function what would query the original function >> call arguments no matter what happens in the function? > > The match.call() function does that, but I think you don't need that: > > f <- function(x = 1, ...) { > x <- 2 > match.call() > } > f(3) > ## Prints: f(x = 3) > > Duncan Murdoch > >> >> Sebastien >> >> On 3/24/2016 8:47 AM, Jeff Newmiller wrote: >> > Don't provide a default value for c in the parameter list. Then you >> > can use the missing() function to make decisions, including whether to >> > generate a default value for c or not. >> > -- >> > Sent from my phone. Please excuse my brevity. >> > >> > On March 24, 2016 5:11:43 AM PDT, sbihorel >> > <Sebastien.Bihorel at cognigencorp.com> wrote: >> > >> > Hi, >> > >> > Please consider the following functions: >> > >> > myf1 <- function(x,fun){ >> > if (is.null(fun)){ >> > x >> > } else { >> > do.call(fun, list(x)) >> > } >> > } >> > >> > myf2 <- function(a=1, b=2, fun=NULL, c=myfun1(b,fun)){ >> > >> > if (myf1(b,fun)>0 & <c was not provided in function call>){ >> > c <- b >> > } >> > print(list(a,b,c)) >> > } >> > >> > myf2(a=2,b=3,fun=exp) >> > myf2(a=2,b=3,fun=exp,c=4) >> > >> > I need to replace "<c was not provided in function call>" by >> some code >> > that would make myf2 aware of it own calls and of the fact that >> the c >> > argument was provided or not in the function calls. >> > >> > Thanks for your help >> > >> > Sebastien >> > >> > >> ------------------------------------------------------------------------ >> > >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> > https://stat.ethz.ch/mailman/listinfo/r-help >> > PLEASE d >> > o read >> > the posting guidehttp://www.R-project.org/posting-guide.html >> > and provide commented, minimal, self-contained, reproducible code. >> > >> -- Sebastien Bihorel Associate Director, Pharmacometrics Buffalo Office: >> +1-716-633-3463 ext. 323 | Website <http://www.cognigencorp.com> >> <http://www.simulations-plus.com/Default.aspx> >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> 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. >-- Sebastien Bihorel Associate Director, Pharmacometrics Buffalo Office: +1-716-633-3463 ext. 323 | Website <http://www.cognigencorp.com> <http://www.simulations-plus.com/Default.aspx>
S Ellison
2016-Mar-24 15:16 UTC
[R] How to make a function aware of its own call arguments
> Are you aware of any function what would query the original function call > arguments no matter what happens in the function?Use missing() first. If you can't use missing() first, or use it early in a parent function and pass a flag, you could perhaps pass a copy of the parent function call to the daughter function and see if an argument was included in the call. Example: f1 <- function(x, ...) { if(missing(x)) x<-3 f2(x, match.call()) } f2 <- function(x, f1.call) { testx <- if(is.null(f1.call$x)) "x was absent in f1" else "x specified in f1" xval <- paste("x now set to", x) cat(sprintf("%s\n%s\n",testx, xval)) } f1() f1(x=2) ******************************************************************* This email and any attachments are confidential. Any use...{{dropped:8}}