methods(class = class(<obj>)) - improve for |cl.| > 1 ?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

methods(class = class(<obj>)) - improve for |cl.| > 1 ?

Martin Maechler
With new "strict" settings in R-devel, the following glm() example

> data(anorexia, package = "MASS")
> fm <- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian, data = anorexia)
> methods(class = class(fm))
Warning in grep(name, row.names(info)) :
  argument 'pattern' has length > 1 and only the first element will be used
Warning in gsub(name, "", row.names(info)) :
  argument 'pattern' has length > 1 and only the first element will be used
Warning in grep(pattern, all.names, value = TRUE) :
  argument 'pattern' has length > 1 and only the first element will be used
Warning in grep(pattern, all.names, value = TRUE) :
  argument 'pattern' has length > 1 and only the first element will be used
...........
...........
...........
[ca. 20 lines of warnings]

and then shows the "glm" methods, but not "lm" ones.

This is not a bug strictly, as  ?methods says that

   class: a symbol or character string naming a class: only used if
          ‘generic.function’ is not supplied.

and so the use of

          methods(class = class(<obj>))

is a user error when  class(<obj>)  is of length > 1.

In the case of e.g. a randomForest() result, we also get 25
warnings, i.e. 50 lines, but then

--->>>  no methods found

because

   > class(rf.fit)
   [1] "randomForest.formula" "randomForest"

and no methods are  defined for "randomForest.formula".

---

Of course, all this works fine with S4 classes:  There the full
inheritance is used and all methods are found.

Still, would it make sense to improve the underlying .S3methods() ?

I assume it will break *some* overzealous package checks out
there when .S3methods() and hence methods() would return *more*
in such case.

Comments?

--
Martin Maechler
ETH Zurich  and  R Core

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: methods(class = class(<obj>)) - improve for |cl.| > 1 ?

Kevin Ushey
I think this would be a good change. I think most users use the
'methods(class = <...>)' function to answer the question, "what
methods can I call on objects with these classes?", and in that
context I think it would be sensible for the function to accept more
than one class.

Kevin

On Wed, Oct 17, 2018 at 7:15 AM Martin Maechler
<[hidden email]> wrote:

>
> With new "strict" settings in R-devel, the following glm() example
>
> > data(anorexia, package = "MASS")
> > fm <- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian, data = anorexia)
> > methods(class = class(fm))
> Warning in grep(name, row.names(info)) :
>   argument 'pattern' has length > 1 and only the first element will be used
> Warning in gsub(name, "", row.names(info)) :
>   argument 'pattern' has length > 1 and only the first element will be used
> Warning in grep(pattern, all.names, value = TRUE) :
>   argument 'pattern' has length > 1 and only the first element will be used
> Warning in grep(pattern, all.names, value = TRUE) :
>   argument 'pattern' has length > 1 and only the first element will be used
> ...........
> ...........
> ...........
> [ca. 20 lines of warnings]
>
> and then shows the "glm" methods, but not "lm" ones.
>
> This is not a bug strictly, as  ?methods says that
>
>    class: a symbol or character string naming a class: only used if
>           ‘generic.function’ is not supplied.
>
> and so the use of
>
>            methods(class = class(<obj>))
>
> is a user error when  class(<obj>)  is of length > 1.
>
> In the case of e.g. a randomForest() result, we also get 25
> warnings, i.e. 50 lines, but then
>
> --->>>  no methods found
>
> because
>
>    > class(rf.fit)
>    [1] "randomForest.formula" "randomForest"
>
> and no methods are  defined for "randomForest.formula".
>
> ---
>
> Of course, all this works fine with S4 classes:  There the full
> inheritance is used and all methods are found.
>
> Still, would it make sense to improve the underlying .S3methods() ?
>
> I assume it will break *some* overzealous package checks out
> there when .S3methods() and hence methods() would return *more*
> in such case.
>
> Comments?
>
> --
> Martin Maechler
> ETH Zurich  and  R Core
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: methods(class = class(<obj>)) - improve for |cl.| > 1 ?

Gabe Becker
Martin and Kevin,

Perhaps a variant of methods which more directly addresses the use-case
Kevin mentions might be in order?

I am happy to prepare a patch which implements a methodsForObj function
(name very much negotiable), or a third obj argument to methods which takes
the actual object and answers the question "what methods would I hit for an
object just like this one?" directly. Is this something you (Martin, et al
in R core), are interested in and would consider?

I know people can always do methods(class = class(obj)) after the change
being discussed (and contingent on that change what I described could be
trivially implemented that way), but  should they need to?

Best,
~G



On Fri, Oct 19, 2018 at 10:55 AM, Kevin Ushey <[hidden email]> wrote:

> I think this would be a good change. I think most users use the
> 'methods(class = <...>)' function to answer the question, "what
> methods can I call on objects with these classes?", and in that
> context I think it would be sensible for the function to accept more
> than one class.
>
> Kevin
>
> On Wed, Oct 17, 2018 at 7:15 AM Martin Maechler
> <[hidden email]> wrote:
> >
> > With new "strict" settings in R-devel, the following glm() example
> >
> > > data(anorexia, package = "MASS")
> > > fm <- glm(Postwt ~ Prewt + Treat + offset(Prewt), family = gaussian,
> data = anorexia)
> > > methods(class = class(fm))
> > Warning in grep(name, row.names(info)) :
> >   argument 'pattern' has length > 1 and only the first element will be
> used
> > Warning in gsub(name, "", row.names(info)) :
> >   argument 'pattern' has length > 1 and only the first element will be
> used
> > Warning in grep(pattern, all.names, value = TRUE) :
> >   argument 'pattern' has length > 1 and only the first element will be
> used
> > Warning in grep(pattern, all.names, value = TRUE) :
> >   argument 'pattern' has length > 1 and only the first element will be
> used
> > ...........
> > ...........
> > ...........
> > [ca. 20 lines of warnings]
> >
> > and then shows the "glm" methods, but not "lm" ones.
> >
> > This is not a bug strictly, as  ?methods says that
> >
> >    class: a symbol or character string naming a class: only used if
> >           ‘generic.function’ is not supplied.
> >
> > and so the use of
> >
> >            methods(class = class(<obj>))
> >
> > is a user error when  class(<obj>)  is of length > 1.
> >
> > In the case of e.g. a randomForest() result, we also get 25
> > warnings, i.e. 50 lines, but then
> >
> > --->>>  no methods found
> >
> > because
> >
> >    > class(rf.fit)
> >    [1] "randomForest.formula" "randomForest"
> >
> > and no methods are  defined for "randomForest.formula".
> >
> > ---
> >
> > Of course, all this works fine with S4 classes:  There the full
> > inheritance is used and all methods are found.
> >
> > Still, would it make sense to improve the underlying .S3methods() ?
> >
> > I assume it will break *some* overzealous package checks out
> > there when .S3methods() and hence methods() would return *more*
> > in such case.
> >
> > Comments?
> >
> > --
> > Martin Maechler
> > ETH Zurich  and  R Core
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



--
Gabriel Becker, Ph.D
Scientist
Bioinformatics and Computational Biology
Genentech Research

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel