パッケージでS3メソッドを使用しようとしていますが、ここで質問した後、セットアップ方法を理解したと思いました。Roxygenを使用してRパッケージをビルドするときのS3メソッドの整合性の警告
しかし、今では予期しない結果が得られます。以下のコードをRで直接実行すると、期待どおりの結果が得られますが、パッケージにコンパイルすると、正しい結果が得られません(から一意の単語のみを取得する必要がある場合に、単語が2回カウントされることに注意してください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