Is it necessary to export base method extensions in an R package? Documentation implications?

前端 未结 1 576
忘掉有多难
忘掉有多难 2021-01-19 12:17

In principle, I could keep these extensions not-exported, and this would also allow me to not-add redundant documentation for these already well-documented methods, while st

1条回答
  •  孤城傲影
    2021-01-19 12:41

    If you don't export the methods, then users (either at the command line or trying to use your classes and methods in their own package via imports) won't be able to use them -- your class will be displayed with the show,ANY-method.

    You are not documenting the generic show, but rather the method appropriate for your class, show,myPackageSpecialClass-method. If in your NAMESPACE you

    import(methods)
    exportMethods(show)
    

    (note that there is no way to export just some methods on the generic show) and provide no documentation, R CMD check will complain

    * checking for missing documentation entries ... WARNING
    Undocumented S4 methods:
      generic 'show' and siglist 'myPackageSpecialClass'
    All user-level objects in a package (including S4 classes and methods)
    should have documentation entries.
    See the chapter 'Writing R documentation files' in the 'Writing R
    Extensions' manual.
    

    Your example (I know it was not meant to be a serious show method :) ) is a good illustration for why methods might be documented -- explaining to the user why every time they try and display the object they get NA, when they were expecting some kind of description about the object.

    One approach to documentation is to group methods with the class into a single Rd file, myPackageSpecialClass-class.Rd. This file would contain an alias

    \alias{show,myPackageSpecialClass-method}
    

    and a Usage

    \S4method{show}{myPackageSpacialClass}(object)
    

    This works so long as no fancy multiple dispatch is used, i.e., it is clear to which class a method applies. If the user asks for help with ?show, they are always pointed toward the methods package help page. For help on your methods / class, they'd need to ask for that specific type of help. There are several ways of doing this but my favorite is

    class ? myPackageSpecialClass
    method ? "show,myPackageSpecialClass"
    

    This will not be intuitive to the average user; the (class|method) ? ... formulation is not widely used, and the specification of "generic,signature" requires a lot of understanding about how S4 works, including probably a visit to selectMethod(show, "myPackageSpecialClass") (because the method might be implemented on a class that myPackageSpecialClass inherits from) or showMethods(class="myPackageSpecialClass", where=getNamespace("myPackage")) (because you're wondering what you can do with myPackageSpecialClass).

    0 讨论(0)
提交回复
热议问题