使用roxygen2记录S4类和方法

时间:2018-12-06 11:36:28

标签: r

我创建一个S4类并为该类编写一个方法。我使用roxygen2文档来记录此类。以下是我的编码和文档示例。

#' Class Clustering
#' Partition data into clusters
#' @name Clustering-class
#' @rdname Clustering-class
#'
#' @slot Cluster List of matrices, where each element of the list grasps data belongs to a single cluster as a matrix
#' @slot Centers Matrix of cluster centers
#' @slot k No of centers
#'
#' @import flexclust clusterGeneration
#' @exportClass Clustering
#' @export Clustering

Clustering <- setClass("Clustering",
                   slots = c(
                       Cluster = "list",
                       k = "numeric",
                       Centers = "matrix"
                   )
)

#' Clustering.
#' @name Clusters
#' @rdname clusters-methods
setGeneric("Clusters",
       def = function(object, x, k)
       {
           standardGeneric("Clusters")
       }
 )

#' Partition data into clusters
#' @description Initilaize slots of class \code{Clustering} by partitioning data set into 'k' clusters.
#'
#' @rdname Clusters-methods
#' @aliases Clusters,Clustering-method
#'
#' @param object An object of class \code{Clustering}.
#' @param x Numeric matrix of data.
#' @param k Number of centers.
#'
#' @details Runs 'flexclust' clustering algorithm with default settings i.e.
#' method = "kmeans", dist = "euclidean", and partition the data set. 

#' @importFrom flexclust cclust
#' @exportMethod Clusters
#'
#' @examples
#'  x <- rbind(matrix(rnorm(150, mean = 5, sd = 0.5), ncol = 2),
#'             matrix(rnorm(100, mean = 15, sd = 0.5), ncol = 2),
#'             matrix(rnorm(200, mean = 30, sd = 0.5), ncol = 2))

#'  obj <- Clustering()
#'
#'  Clusters(obj, x = x, k = 3)
#'
#' @return An object of class \code{Clustering}
#'
setMethod("Clusters",
      signature(object = "Clustering", x = "matrix", k = "numeric"),
      definition = function(object, x, k)
      {
          fit <- flexclust::cclust(x, k, dist = "euclidean", method =  "kmeans")
          for(i in 1:k)
              object@Cluster[[i]] <- x[fit@cluster==i,]
          object@Centers <- fit@centers
          object@k <- k
          return(object)
      }
)

当我检查包裹时,它会给我警告消息

  

检查缺少的文档条目...警告         未公开的代码对象:        “集群”         未记录的S4类:        “聚类”        程序包中的所有用户级对象(包括S4类和方法)        应该有文档条目。        请参阅“编写R”中的“编写R文档文件”一章        扩展手册。   我不知道该如何解决该问题以及缺少哪个roxygen2文档。

0 个答案:

没有答案