Skip to content

Error: object of type 'closure' is not subsettable

4 messages · Matthew Walker, Gabor Grothendieck, Rolf Turner +1 more

#
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
#
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:
3 days later
#
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:

            
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
#
On 01/17/2010 09:00 PM, Rolf Turner wrote:
rep is an internal generic, so the  dispatch happens internally (in the 
c code). Here is the relevant C code fragment :

SEXP attribute_hidden do_rep(SEXP call, SEXP op, SEXP args, SEXP rho)
{
     SEXP ans, x, ap, times = R_NilValue /* -Wall */, ind;
     int i, lx, len = NA_INTEGER, each = 1, nt, nprotect = 4;

     if (DispatchOrEval(call, op, "rep", args, rho, &ans, 0, 0))
	return(ans);

     ...

}

Romain