On 01/17/2010 09:00 PM, Rolf Turner wrote:
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)?

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

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.walke...@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

--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/KfKn : Rcpp 0.7.2
|- http://tr.im/JOlc : External pointers with Rcpp
`- http://tr.im/JFqa : R Journal, Volume 1/2, December 2009

______________________________________________
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