[Rd] bugs in head() and tail()

Gabriel Becker g@bembecker @end|ng |rom gm@||@com
Tue Mar 26 05:58:37 CET 2019


Hi Abs,

This is because the class is "f", not c("f", "function") in your second
example. S3 method dispatch is doing what you (unintentionally, I presume)
asked it to do.

The S3 method which allows head to take functions is utils:::head.function.
S3 can only be expected to  understand inheritance if the value of class(f)
is multivalued. It doesn't have any way of knowing  f is still a function
after the class assignment because "function" does not appear anywhere in
the class vector, so instead of hitting utils:::head.function, it hits
utils:::head.default, which uses [ on the argument, causing the error.

I'd say this is "expected" behavior within the context of the S3 system.

I also see this behavior at least as far aback as 3.5.1, so its not new to
3.5.3.

Best,
~G

On Mon, Mar 25, 2019 at 8:44 PM Abs Spurdle <spurdle.a using gmail.com> wrote:

> (Using R 3.5.3).
>
> I found bugs in head() and tail().
>
> The following works:
>
> > f = function () 1
> > head (f)
> 1 function ()
> 2 1
>
> However, the following does not:
>
> > class (f) = "f"
> > head (f)
> Error in x[seq_len(n)] : object of type 'closure' is not subsettable
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



More information about the R-devel mailing list