我试图在一个包中使用S3方法,并认为我在这里问一个问题之后理解了如何设置它:使用Roxygen构建R包时的S3方法一致性警告
但现在我得到的结果我没想到.如果我直接在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)
Run Code Online (Sandbox Code Playgroud)
从命令行运行代码(预期行为):
> 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
Run Code Online (Sandbox Code Playgroud)
包编译后的输出:
> 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
Run Code Online (Sandbox Code Playgroud)
had*_*ley 13
您看到的错误很可能是因为没有导出公共通用的列表方法,因此common.default会被调用.您可以使用以下方法来解决此问题devtools::missing_s3- 该函数有点启发式,因此您可能会得到一些误报(例如,它目前无法判断这is.list不是一种方法).这是一个非常常见的问题(它已经抓住了我很多次),并且下一次roxygen迭代会做更多的事情来防止它.
目前,要使用roxygen正确导出S3方法,您需要执行以下任一操作:
@S3method generic class (如果您不想记录该方法,则别无其他)@method generic class而 @export如果你要导出并记录.你永远不应该@S3method和@method相同的文档块.
现在,roxygen会自动判断函数是否为S3方法,因此:
@S3method或@method@export,如果你想导出的方法(你通常做,即使通用不)| 归档时间: |
|
| 查看次数: |
1371 次 |
| 最近记录: |