Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

r s3 - S3 method consistency warning when building R package with Roxygen

I have created a roxygen file for a function that uses S3 an class. I roxygenize and then build and check and get a warning:

* checking S3 generic/method consistency ... WARNING
common:
  function(word.list, ...)
common.list:
  function(word.list, overlap, equal.or)

See section 'Generic functions and methods' of the 'Writing R
Extensions' manual.

So I spent time studying:

http://cran.r-project.org/doc/manuals/R-exts.html#Generic-functions-and-methods & https://github.com/hadley/devtools/wiki/S3

But I can't figure out what I've done wrong in the file below. The function works as expected.
1) why is the warning occurring? 2) how can I make it go away?

#' 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
#' 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 <-
function(word.list, ...){
    UseMethod("common")
}

#' @return code{NULL}
#'
#' @rdname common
#' @method common list
#' @S3method 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 = "equal"){
        LIS <- list(...)
        return(common.list(LIS, overlap, equal.or))
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think this happens because a method must have all the same arguments as the generic. So add ... to the arguments of common.list(). Like this:

common.list <- function(word.list, overlap = "all", equal.or = "more", ...)

Similarly, common.default() should have word.list as an argument.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...