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]]

______________________________________________
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