Actually, this version is more general, doesn't need to know the name of the
function:
f = function(x,y){
    curfun = deparse(match.call()[1])
    curfun = substr(curfun,1,nchar(curfun)-2)
    if(length(formals(curfun))!=nargs())
        stop('Something is missing...')
}
f()


Putting this code at the top of every function was be a check to make sure
that current arguments are always supplied.

It would be nice if there were a way to see which ones were missing (if
any), but this will do.

Thanks.  Good to know the details about "[" and that it's tolerant of
missing subsets.


On Mon, Sep 26, 2011 at 4:33 PM, Gene Leynes <gley...@gmail.com> wrote:

> Alan and Duncan,
>
> 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...
>>
>
> Absolutely.  That wasn't the way I wanted to do it, but I didn't see
> "force" in the help when reading any of the many different help topics.
>
> If you wrote the function, you should know what its args are, so you could
>> force them:
>>
>
> Yes, for a particular function I do know the arguments, but I wanted
> something more flexible so that if I change something I don't have to
> rewrite new tests.
>
>
> For your case, try something like
>>     myfun <- function(vec, i){ i <- as.integer(i); vec[i]; }
>> which will throw the error.
>>
>
>
> Good idea.  It's not as flexible as I was looking for, but it does check
> type, which is a benefit if I'm going to go to the trouble of testing each
> variable manually.
>
> I guess this is pretty close to what I want, I didn't think of "formals" at
> first.
>
> f = function(x,y){
>     if(nargs()!=length(formals(f)))
>         stop('Something is missing...')
> }
> f()
>
>
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to