S3方法的帮助(roxygen2)(S3 method help (roxygen2))

2019-08-07 02:25发布

我想在一个包使用S3方法,我想我明白如何问问题后,在这里设置它: S3方法一致性Roxygen建设[R包时警告

但现在我得到的结果,我不指望。 如果我的正下方R中运行代码它给我了预期的效果,但如果我把它编译成一个包我没有得到正确的结果(注意被计算两次,当它应该只需要独特的词的词vector a ) 。 我不知道什么我不正确设置。

该.R文件:

#' Find Common Words Between Groups
#' 
#' Find common words between grouping variables (e.g. people).
#' 
#' @param word.list A list of names chacter vectors.
#' @param overlap Minimum/exact amount of overlap.
#' @param equal.or A character vector of c(\code{"equal"}, \code{"greater"}, 
#' \code{"more"}, \code{"less"}).
#' @param \dots In liu of word.list the user may input n number of character 
#' vectors.
#' @rdname common
#' @return Returns a dataframe of all words that match the criteria set by 
#' \code{overlap} and \code{equal.or}.
#' @export
#' @examples
#' \dontrun{
#' a <- c("a", "cat", "dog", "the", "the")                                                              
#' b <- c("corn", "a", "chicken", "the")                                                                
#' d <- c("house", "feed", "a", "the", "chicken")                                                       
#' common(a, b, d, overlap=2)  
#' common(a, b, d, overlap=3)                                                                          
#'                                                                                                      
#' r <- list(a, b, d)  
#' common(r)                                                                                 
#' common(r, overlap=2)                                                                                            
#'                                                                                                     
#' common(word_list(DATA$state, DATA$person)$cwl, overlap = 2) 
#' } 
common <-
function(word.list, ...){
    UseMethod("common")
}

#' @return \code{NULL}
#'
#' @rdname common
#' @method common list
common.list <-
function(word.list, overlap = "all", equal.or = "more", ...){
    if(overlap=="all") {
        OL <- length(word.list) 
    } else {
        OL <- overlap
    }
    LIS <- sapply(word.list, unique)
    DF <- as.data.frame(table(unlist(LIS)), stringsAsFactors = FALSE)
    names(DF) <- c("word", "freq")
    DF <- DF[order(-DF$freq, DF$word), ]
    DF <- switch(equal.or,
        equal = DF[DF$freq == OL, ],
        greater = DF[DF$freq > (OL - 1), ],
        more = DF[DF$freq > (OL - 1), ],
        less = DF[DF$freq < (OL + 1), ])
    rownames(DF) <- 1:nrow(DF)
    return(DF)
}

#' @return \code{NULL}
#'
#' @rdname common
#' @method common default
#' @S3method common default  
common.default <-
    function(..., overlap = "all", equal.or = "more", word.list){
        LIS <- list(...)
        return(common.list(LIS, overlap, equal.or))
}


a <- c("a", "cat", "dog", "the", "the")                                                              
b <- c("corn", "a", "chicken", "the")                                                                
d <- c("house", "feed", "a", "the", "chicken")                                                       
common(a, b, d, overlap=2)  


r <- list(a, b, d)                                                                                   
common(r, overlap=2)                                                                                            

运行命令行(预期的行为)的代码:

> common(a, b, d, overlap=2)  
     word freq
1       a    3
2     the    3
3 chicken    2
>                                                                           
>                                                                                                      
> r <- list(a, b, d)                                                                                   
> common(r, overlap=2)                                                                                            
     word freq
1       a    3
2     the    3
3 chicken    2

包编译后的输出:

> a <- c("a", "cat", "dog", "the", "the")                                                              
> b <- c("corn", "a", "chicken", "the")                                                                
> d <- c("house", "feed", "a", "the", "chicken")                                                       
> common(a, b, d, overlap=2)  
     word freq
1       a    3
2     the    3
3 chicken    2
>                                                                           
>                                                                                                      
> r <- list(a, b, d)                                                                                   
> common(r, overlap=2)                                                                                            
     word freq
1     the    4
2       a    3
3 chicken    2

Answer 1:

您所看到的错误是最有可能造成因为共同的通用列表方法不被出口,所以common.default被调用来代替。 你可以拿起利用这个问题devtools::missing_s3 -功能有点启发,所以你可能会得到一些假阳性(例如,它目前还不能告诉大家, is.list不是方法)。 这是一个令人难以置信的常见问题(它已经抓住了我这么多次),以及roxygen的下一次会做更多,以防止它。

目前,要正确出口与roxygen你需要做的要么S3方法:

  • @S3method generic class (而不是其他),如果你不希望文件鉴别方法
  • @method generic class@export如果要导出并记录。

你永远不应该有@S3method@method在同一个文档块。

更新roxygen2> 3.0.0

现在roxygen自动判断一个函数是S3方法,以便:

  • 切勿使用@S3method@method
  • 使用@export如果你要导出的方法(你通常做,即使通用不)


文章来源: S3 method help (roxygen2)
标签: r roxygen2 r-s3