Re: [R] Testing for arguments in a function

2011-09-30 Thread Paul Johnson
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 P

Re: [R] Testing for arguments in a function

2011-09-28 Thread Paul Johnson
On Mon, Sep 26, 2011 at 2: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)

Re: [R] Testing for arguments in a function

2011-09-26 Thread Duncan Murdoch
On 11-09-26 8:49 PM, David Winsemius wrote: On Sep 26, 2011, at 8:04 PM, Duncan Murdoch wrote: On 11-09-26 5:15 PM, David Winsemius wrote: On Sep 26, 2011, at 4:56 PM, Duncan Murdoch wrote: On 26/09/2011 3:39 PM, Gene Leynes wrote: I don't understand how this function can subset by i when

Re: [R] Testing for arguments in a function

2011-09-26 Thread David Winsemius
On Sep 26, 2011, at 8:04 PM, Duncan Murdoch wrote: On 11-09-26 5:15 PM, David Winsemius wrote: On Sep 26, 2011, at 4:56 PM, Duncan Murdoch wrote: 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 = fu

Re: [R] Testing for arguments in a function

2011-09-26 Thread Gene Leynes
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,

Re: [R] Testing for arguments in a function

2011-09-26 Thread Gene Leynes
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

Re: [R] Testing for arguments in a function

2011-09-26 Thread Duncan Murdoch
On 11-09-26 5:15 PM, David Winsemius wrote: On Sep 26, 2011, at 4:56 PM, Duncan Murdoch wrote: 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 da

Re: [R] Testing for arguments in a function

2011-09-26 Thread Gabor Grothendieck
On Mon, Sep 26, 2011 at 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)

Re: [R] Testing for arguments in a function

2011-09-26 Thread David Winsemius
On Sep 26, 2011, at 4:56 PM, Duncan Murdoch wrote: 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

Re: [R] Testing for arguments in a function

2011-09-26 Thread Duncan Murdoch
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

[R] Testing for arguments in a function

2011-09-26 Thread Gene Leynes
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