[R] class export in package creation / setClass / namespace?

Martin Morgan mtmorgan at fhcrc.org
Tue Jul 21 03:57:33 CEST 2009


L L <lmlahti at gmail.com> writes:

> Ok, I could solve also the latter problem by defining show.myclass function in
> the zzz.R file and adding the line 'S3method(show,myclass)' into NAMESPACE
> file. Now the package passes all checks.

I would have, in NAMESPACE,

  importFrom(methods, show)
  exportMethods(show)

and in some file in R/

  setMethod(show, "myclass", function(object) {
      cat("here I am\n")
   })

Do not try to make an S3 method on an S4 generic, or to create a new
S4 generic for show. Since you've defined a method on show, you need
to add documentation in a file in man/ (your choice; I'd add the
documentation to the myclass documentation page). You only get one
\alias{show,myclass-method}.

Martin

> The information on how to exactly extend existing methods and include new
> methods/classes into a package is available but rather scattered in the web. A
> step-by-step tutorial written by an experienced user who is aware of best
> practices etc. would be rather useful for a beginner.
> best regards
> Leo
>
>
> On Mon, Jul 20, 2009 at 7:09 PM, L L <[[lmlahti at gmail.com]]> wrote:
>
>           Thanks, the issue was solved by adding class definitions to the
>      zzz.R file in the R code directory. However, this led to a new
>      problem.     
>      The zzz.R now contains class definition:     
>      > setClass("myclass", contains = "list")     
>      and  method definition for the new class, extending the generic
>      show':     
>      > setGeneric("show",function(x,...){standardGeneric("show")})     >
>      setMethod("show", "myclass",function(x, ...) {cat("myclass object
>      \n")})     
>      I get two warnings. The first one:     
>      * checking Rd files ... WARNING     Rd files with duplicated alias
>      show,myclass-method':       myclass-class.Rd show-methods.Rd     
>      I get this one because the alias row for the method
>      (\alias{show,myclass-method}) is in both myclass-class.Rd file and
>      show-methods.Rd file (created by promptMethods function). This is
>      likely related to the second warning:     
>      * checking for missing documentation entries ... WARNING     Undocumented
>      code objects:       show     
>      I thought that the show method would've been documented correctly as
>      I put 'show-methods.Rd' file in the 'man' directory. This does not
>      seem to be the case, however.     
>      Any help on how I should document the extended show method in this
>      case, or are there some mistakes in my original definition for
>      extended 'show'? I could not find suitable examples from the
>      web/mailing lists.     
>      br     Leo
>           
>                
>      
>      
>
>           On Fri, Jul 10, 2009 at 5:37 AM, Martin Morgan
>      <[[mtmorgan at fhcrc.org]]> wrote:     
>
>                               L L wrote:          > Dear all,          >          >
>           I have been trying to create an R package. This has been
>           successfull until I          > tried to define classes.          >          >
>           Currently, my procedure is the following:          >          >
>           Start R, load the function and class definition          >>
>           tmp <- function (x) {x}          >> setClass("rpa",
>           contains = "list", where=topenv(parent.frame()))          >          >
>           Use package skeleton to create directory structure:          >>
>           package.skeleton(name = "test",list=c("tmp"))          
>           
>
>
>           I don't think package.skeleton knows to copy the class
>           definition to the          R files; I think you have to do
>           that yourself -- is there a          setClass("rpa",
>           contains="list") instruction in the R files created by          package.skeleton?          
>           Martin          
>
>                     >          > Edit man files, add test/NAMESPACE
>           file with the following contents:          >> export(tmp)          >>
>           exportClasses("rpa")          >          > Use R CMD check
>           and R CMD build to create tarball.          > -> No error
>           messages.          >          > Start R, install the
>           created package:          >>
>           install.packages("test_1.0.tar.gz",repos=NULL)          >          >
>           Test the new package:          >> require(test)          >>
>           my.object = new("rpa")          >          > This gives
>           the error message          >> Error in getClass(Class,
>           where = topenv(parent.frame())) :          >> "rpa" is not
>           a defined class          >          > Any tips on how to
>           define and export global classes in package creation?          >          >
>           I also tried          > - to replace the
>           topenv(parent.frame())' with '.GlobalEnv' in setClass.          >
>           - place the setClass within function 'tmp'          > -
>           exportClasses(rpa) in the namespace (without suffixes "")          >          >
>           I paged through the related mailing list discussions but
>           could not find a          > solution. Any tips and tricks
>           would be worthwhile now; I guess this should          > be
>           a standard issue but it seems a bit tricky to come up with
>           a quick          > solution with the little programming
>           experience that I have.          >          > kind regards          >
>           Leo Lahti          >          
>
>
>           >       [[alternative HTML version deleted]]          >          >
>           ______________________________________________          >
>           [[R-help at 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.          
>           
>
>
>           
>
>
>
>

-- 
Martin Morgan
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793




More information about the R-help mailing list