I don't understand how this function can subset by i when i is missing.... ## My function: myfun = function(vec, i){ ret = vec[i] ret } ## My data: i = 10 vec = 1:100 ## Expected input and behavior: myfun(vec, i) ## Missing an argument, but error is not caught! ## How is subsetting even possible here??? myfun(vec) Is there a way to check for missing function arguments, *and* which function arguments are missing? For example myfun = function(vec, i){ curArgs = current.function.arguments() if(any(sapply(curArgs, missing))){ stop() } ret = vec[i] ret } Obviously "current.function.arguments()" is imaginary, but is there something that would return the current arguments in a way that could be passed to "missing()"?? I tried this: curfun = substr(match.call()[1],1,nchar(match.call()[1])) curargs = strsplit(deparse(args(curfun)),',')[[1]] curargs = gsub('function|\\(| |\\)','',curargs) sapply(curargs,missing(x)) and this: sapply(curargs,function(txt) eval(substitute(missing(x), list(x=txt)))) inside the function, but missing doesn't like it when you do anything but call it directly Using R 2.13.0 on a Windows 7 machine [[alternative HTML version deleted]]
On 26/09/2011 3:39 PM, Gene Leynes wrote:> I don't understand how this function can subset by i when i is missing.... > > ## My function: > myfun = function(vec, i){ > ret = vec[i] > ret > } > > ## My data: > i = 10 > vec = 1:100 > > ## Expected input and behavior: > myfun(vec, i) > > ## Missing an argument, but error is not caught! > ## How is subsetting even possible here??? > myfun(vec)Subsetting allows missing arguments. What you have is equivalent to evaluating vec[] which is legal.> > Is there a way to check for missing function arguments, *and* which function > arguments are missing? > > For example > myfun = function(vec, i){ > curArgs = current.function.arguments() > if(any(sapply(curArgs, missing))){ > stop() > } > ret = vec[i] > ret > }> Obviously "current.function.arguments()" is imaginary, but is there > something that would return the current arguments in a way that could be > passed to "missing()"?? > > > I tried this: > curfun = substr(match.call()[1],1,nchar(match.call()[1])) > curargs = strsplit(deparse(args(curfun)),',')[[1]] > curargs = gsub('function|\\(| |\\)','',curargs) > sapply(curargs,missing(x)) > and this: > sapply(curargs,function(txt) eval(substitute(missing(x), list(x=txt)))) > > inside the function, but missing doesn't like it when you do anything but > call it directlyIf you wrote the function, you should know what its args are, so you could force them: > myfun function(vec, i){ force(vec) force(i) ret = vec[i] ret } > myfun(vec) Error in force(i) : argument "i" is missing, with no default or test them explicitly with missing(). If you want to do this automatically, then you shouldn't be using substrings and deparse, you should work at the language level. But I don't see the reason you want to do this... Duncan Murdoch> > Using R 2.13.0 on a Windows 7 machine > > [[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, Sep 26, 2011 at 3:39 PM, Gene Leynes <gleynes at gmail.com> wrote:> I don't understand how this function can subset by i when i is missing.... > > ## My function: > myfun = function(vec, i){ > ? ?ret = vec[i] > ? ?ret > } > > ## My data: > i = 10 > vec = 1:100 > > ## Expected input and behavior: > myfun(vec, i) > > ## Missing an argument, but error is not caught! > ## How is subsetting even possible here??? > myfun(vec) > > > Is there a way to check for missing function arguments, *and* which function > arguments are missing? >R lets you pass missing arguments down from one function call to another in such a way that they retain their missingness:> f <- function(x) g(x) > g <- function(x) missing(x) > f()[1] TRUE -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
On Mon, Sep 26, 2011 at 2:39 PM, Gene Leynes <gleynes at gmail.com> wrote:> I don't understand how this function can subset by i when i is missing.... > > ## My function: > myfun = function(vec, i){ > ? ?ret = vec[i] > ? ?ret > } > > ## My data: > i = 10 > vec = 1:100 > > ## Expected input and behavior: > myfun(vec, i) > > ## Missing an argument, but error is not caught! > ## How is subsetting even possible here??? > myfun(vec) >Hello, Gene: It seems to me the discussion of your question launches off into a more abstract direction than we need here. I've found it is wise to name arguments to functions differently than variables in the environment, so you don't have the function looking for i outside itself. And you can put each variable to a ridiculous default to force an error when that option is not given. "NAN" is nothing here, just a string that has no meaning, so we get an error as you said you wanted. myfun <- function(ivec, ii=NAN){ ivec[ii] } myfun(1:40,10) works myfun(1:40) Produces Error in myfun(1:40) : object 'NAN' not found If you are happy enough to just plop out an error, there's no need to worry. Note the function can be written more succinctly than you originally had it, and you are generally advised to use "<-" rather than "=" by the R developers. -- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas
Just for the record, following Bill Dunlap's advice, I think this is the best answer to the question as originally posed is. myfun <- function(vec, i=stop("'i' must be supplied")){ vec[i] }> myfun(1:40,10)[1] 10> myfun(1:10)Error in myfun(1:10) : 'i' must be supplied>-- Paul E. Johnson Professor, Political Science 1541 Lilac Lane, Room 504 University of Kansas