[R] Error: object of type 'closure' is not subsettable

Rolf Turner r.turner at auckland.ac.nz
Sun Jan 17 21:00:54 CET 2010


I thought it would be possible to make rep() work for functions
by writing a method for the function class.  I tried:

rep.function <- function(x,...) {
    times <- as.list(...)[[1]]
    rslt  <- vector("list",times)
    rslt[1:times] <- list(x)
    rslt
}

But then doing

rep(sin,2)

still gave an error --- Error: object of type 'builtin' is not  
subsettable

Note the difference: ``builtin'' rather than ``closure''.

I then noticed that there is no generic function for rep, although
there are ***methods*** for rep.  I don't understand this at all!

So I did:

rep.default <- rep
rep <- function(x,...){UseMethod("rep")}

Having taken these steps, rep(sin,2) worked as expected.

But why doesn't rep() have a generic form?  And how can there
be methods ***without*** a generic?  Can anyone explain the
issues to me, preferably in terms that are comprehensible to
the human mind (which is what I'm equipped with)?

	cheers,

		Rolf Turner

On 14/01/2010, at 2:24 PM, Gabor Grothendieck wrote:

> See ?rep where it says that the argument must be a vector.  Try
>    rep(list(sin), 3)
>
> On Wed, Jan 13, 2010 at 8:11 PM, Matthew Walker
> <matthew.walker.1 at ulaval.ca> wrote:
>> Hi everyone,
>>
>> Would somebody please explain (or point me to a reference that  
>> explains) the
>> following error:
>>
>> "Error: object of type 'closure' is not subsettable"
>>
>> I was trying to use rep() to replicate a function:
>>
>>> example_function <- function() { return(TRUE) }
>>> rep(example_function, 3)
>> Error: object of type 'closure' is not subsettable
>>
>> But I just cannot understand this error.  I can combine functions  
>> using "c"
>> without any problems:
>>
>>> c(example_function, example_function)
>> [[1]]
>> function ()
>> {
>>   return(TRUE)
>> }
>>
>> [[2]]
>> function ()
>> {
>>   return(TRUE)
>> }
>>
>> What am I doing wrong when I use rep()?
>>
>> Thanks in advance,
>>
>> Matthew Walker
>>
>> ______________________________________________
>> 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.
>>
>
> ______________________________________________
> 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.


######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}



More information about the R-help mailing list