S3方法帮助(roxygen2)
我试图在一个包中使用S3方法,在这里问了一个问题后,我认为我理解了如何设置它: 但现在我得到了意想不到的结果。如果我直接在R中运行下面的代码,它会给我预期的结果,但是如果我将其编译到一个包中,我不会得到正确的结果(请注意,当它应该只从S3方法帮助(roxygen2),r,roxygen2,r-s3,R,Roxygen2,R S3,我试图在一个包中使用S3方法,在这里问了一个问题后,我认为我理解了如何设置它: 但现在我得到了意想不到的结果。如果我直接在R中运行下面的代码,它会给我预期的结果,但是如果我将其编译到一个包中,我不会得到正确的结果(请注意,当它应该只从向量a中获取唯一的单词时,单词会被计数两次)。我不确定我的设置是否有误 .R文件: #' Find Common Words Between Groups #' #' Find common words between grouping variables (e.
向量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
查找组之间的常用词
#'
#'查找分组变量(如人)之间的常用词。
#'
#“@param word.list列出名称和字符向量的列表。
#“@param重叠最小/精确重叠量。
#'@param equal.或c(\code{“equal”}、\code{“greater”})的字符向量,
#'\code{“more”},\code{“less”})。
#'@param\word.list的liu中的点用户可以输入n个字符
#“向量。
#'@rdname common
#“@return返回与设置的条件匹配的所有单词的数据帧
#'\code{overlap}和\code{equal.or}。
#“@出口
#“@示例
#“\dontrun{
#”“是的
>r公共线(r,重叠=2)
词频
1和4
2 a 3
3只鸡2只
您看到的错误很可能是由于未导出通用泛型的列表方法引起的,因此调用了common.default
。您可以使用devtools::missing\u s3
-函数有点启发性,因此可能会出现一些误报(例如,它目前无法判断是否为.list
不是一种方法)。这是一个非常常见的问题(我已经多次发现了这个问题),roxygen的下一次迭代将更有效地防止它
目前,要使用roxygen正确导出S3方法,您需要执行以下操作之一:
(无其他内容),如果您不想记录该方法@S3method泛型类
和@方法泛型类
,如果要导出并记录它@导出
@S3method
和@method
roxygen2>3.0.0的更新
现在roxygen会自动判断函数是否为S3方法,因此:
- 切勿使用
或@S3method
@method
- 如果希望导出方法,请使用
(您通常会这样做,即使泛型不是)@export
@s3method
,但不要记录它。如果要导出并记录它,请使用@method
+@export
。您没有记录这些方法,因此它们每个方法都需要一个@s3method
标记,而roxygen块中没有任何其他内容。您看到的bug主要是由因为通用泛型的列表方法没有被导出,所以调用了common.default
。devtools
有缺少的\u s3
启发式方法来帮助检测这个问题。@hadley感谢你的回答。这两个方法都是正确的。我只触及了devtools
的表面nds认为我需要进一步深入。我可以要求你把评论作为一个答案,这样我就可以为未来的搜索者标记它的正确性。