如何使用roxygen导出R中的类(S4)

时间:2015-05-25 15:31:50

标签: r s4

我建立了一个例子:

            #'  class foo
            #'  
            #'  Is basic class for testing
            #'  
            #'  @slot Name a character. 
            #'  @export foo

            foo <- setClass(Class = "foo",
                             slots = c(Name = "character"),prototype = list(Name = character())
            )


            #' foo
            #' 
            #' Tis is a basic constructor for testing
            #' 
            #'  @param Name a character
            #'  
            #'  @export
            #' 

            if(!isGeneric("foo")){
              setGeneric(name = "foo",
                         def = function(Name_ = "default")
                         {
                           standardGeneric("foo")
                         })
            }

            setMethod(f = "foo", signature(Name_ = "character"), 

                      definition = function(Name_)
                      {
                        return(new(Class = foo, Name = Name_))
                      }
            )

然后在另一个文件中我定义了一个setter / getter:

            #' Getter and setter
            #' 
            #' Getter and setter for the class foo (test)
            #' 
            #'  
            #'  @export
            #' 

            if(!isGeneric("getname")){
              setGeneric(name = "getname",
                         def = function(theObject)
                         {
                           standardGeneric("getname")
                         })
            }

            setMethod(f = "getname", signature(theObject = "foo"), 

                      definition = function(theObject)
                      {
                        return(theObject@Name)
                      }
            )

然而,当我运行&#39; devtools :: load_all()&#39;我总是得到错误:

in method for ‘getname’ with signature ‘theObject="foo"’: no definition for class “foo”.

如何正确导出S4类。我跟着哈德利威克姆的tutorial。我希望人们可以构建我的类的实例但不扩展它。

1 个答案:

答案 0 :(得分:2)

当类型约束是来自另一个包的类时,请在setMethod之前写@import <package_name>,其中package_name是定义类的包。如果类属于同一个包,就像你的问题一样,你必须编写@include <file.R>以确保roxygen在使用类作为约束之前处理了你的类定义文件。

希望有所帮助