2017-08-28 75 views
2

記錄時使用S3類作爲S4我有以下上下文:Roxygen2:重載R基本功能(COR)

我做過載cor基函數,使得我在我的包.R文件以下聲明:

#'export 
setGeneric("cor") 

現在我要創建我的對象(類名爲stranger)特定的功能 - 在這裏爲簡單起見,我只考慮我的對象是與名爲.id附加列data.table。

#' Correlation for stranger objects 
#' describeIn cor Correlation method for stranger objects. 
setMethod("cor",signature(x="stranger"),function(x, method = c("pearson", "kendall", "spearman")){ 
    selectMethod("cor","ANY")(x[,-'.id',with=FALSE],y=NULL, use="everything",method=method) 
}) 

如果我understant setGeneric,它依賴於S4類 - 因此signature參數。

不過,我不使用S4類,但建立自己的stranger對象,具有簡單的老辦法:

buildClass <- function(x,...){ 
    #... prepare out object as data.table with .ìd column 
    class(out) <- c("stranger", class(out)) 
    return(out) 
} 

也就是說,我沒有S4類我的對象。 Dispacthing仍然有效:在我的對象調用cor正確應用專用方法。

我的問題是關於適當記錄與ROxygen2。目前,加載我的功能的時候,我碰到下面的信息:

Updating stranger documentation 
Loading stranger 
Creating a generic function for 'cor' from package 'stats' in package 'stranger' 
in method for 'cor' with signature 'x="stranger"': no definition for class "stranger" 

我已經仔細閱讀roxygen2加上一些似乎對計算器的相關問題哈德利的小插曲,但他們只處理或者與古典S3機制或純S4,而我沒有S4的構造與setClasssetGeneric依靠S4。

回答

1

而不是設置一個S4通用方法cor(),可以重新定義它作爲一個通用S3並定義它的方法。爲了說明,我創建的R包僅具有兩個R上的文件, 「buildClass.R」 和 「cor.R」,複製如下:

buildClass.R:

#' Stranger Class Constructor 
#' 
#' Put some details about it 
#' 
#' @param x an object 
#' 
#' @export 
buildClass <- function(x){ 
    class(x) <- c("stranger", class(x)) 
    return(x) 
} 

COR。 [R

#' Cor 
#' 
#' Put some details about it 
#' 
#' @param x an object 
#' @param ... other arguments 
#' 
#' @rdname cor 
#' @export 
cor <- function(x, ...) { 
    UseMethod('cor', x) 
} 

#' @rdname cor 
#' @export 
cor.stranger <- function(x, ...) { 
    return(1) 
} 

#' @rdname cor 
#' @export 
cor.default <- function(x, ...) { 
    return(stats::cor(x, ...)) 
} 

然後,如果你加載你的包(在我的情況「anRpackage」命名),用戶將被警告,包裝口罩stats::cor,但方式cor.default()定義,stats::cor()被稱爲對於不是stranger類的對象:

library(anRpackage) 

Attaching package: ‘anRpackage’ 

The following object is masked from ‘package:stats’: 

    cor 

set.seed(1234) 
regular_mat <- matrix(rnorm(100), nrow = 25) 
stranger_mat <- buildClass(regular_mat) 
cor(regular_mat) 

      [,1]  [,2]  [,3]  [,4] 
[1,] 1.00000000 0.1531414 -0.01948986 -0.3737424 
[2,] 0.15314141 1.0000000 0.17531423 -0.1752925 
[3,] -0.01948986 0.1753142 1.00000000 0.4371213 
[4,] -0.37374237 -0.1752925 0.43712127 1.0000000 

cor(stranger_mat) 
[1] 1 

當使用缺省cran = TRUE(它檢查「使用相同的設置作爲CRAN使用」),沒有錯誤,警告,或票據的devtools::check()檢查包提出了:

> check(current.code) 
Updating anRpackage documentation 
Loading anRpackage 
Setting env vars ---------------------------------------------------------------- 
CFLAGS : -Wall -pedantic 
CXXFLAGS: -Wall -pedantic 
Building anRpackage ------------------------------------------------------------- 
'/usr/lib/R/bin/R' --no-site-file --no-environ --no-save --no-restore --quiet \ 
    CMD build '/home/duckmayr/anRpackage' --no-resave-data --no-manual 

* checking for file ‘/home/duckmayr/anRpackage/DESCRIPTION’ ... OK 
* preparing ‘anRpackage’: 
* checking DESCRIPTION meta-information ... OK 
* checking for LF line-endings in source and make files and shell scripts 
* checking for empty or unneeded directories 
* building ‘anRpackage_1.0.tar.gz’ 

Setting env vars ---------------------------------------------------------------- 
_R_CHECK_CRAN_INCOMING_USE_ASPELL_: TRUE 
_R_CHECK_CRAN_INCOMING_   : FALSE 
_R_CHECK_FORCE_SUGGESTS_   : FALSE 
Checking anRpackage ------------------------------------------------------------- 
'/usr/lib/R/bin/R' --no-site-file --no-environ --no-save --no-restore --quiet \ 
    CMD check '/tmp/RtmpTcdHJ5/anRpackage_1.0.tar.gz' --as-cran --timings \ 
    --no-manual 

* using log directory ‘/tmp/RtmpTcdHJ5/anRpackage.Rcheck’ 
* using R version 3.4.3 (2017-11-30) 
* using platform: x86_64-pc-linux-gnu (64-bit) 
* using session charset: UTF-8 
* using options ‘--no-manual --as-cran’ 
* checking for file ‘anRpackage/DESCRIPTION’ ... OK 
* checking extension type ... Package 
* this is package ‘anRpackage’ version ‘1.0’ 
* checking package namespace information ... OK 
* checking package dependencies ... OK 
* checking if this is a source package ... OK 
* checking if there is a namespace ... OK 
* checking for executable files ... OK 
* checking for hidden files and directories ... OK 
* checking for portable file names ... OK 
* checking for sufficient/correct file permissions ... OK 
* checking whether package ‘anRpackage’ can be installed ... OK 
* checking installed package size ... OK 
* checking package directory ... OK 
* checking DESCRIPTION meta-information ... OK 
* checking top-level files ... OK 
* checking for left-over files ... OK 
* checking index information ... OK 
* checking package subdirectories ... OK 
* checking R files for non-ASCII characters ... OK 
* checking R files for syntax errors ... OK 
* checking whether the package can be loaded ... OK 
* checking whether the package can be loaded with stated dependencies ... OK 
* checking whether the package can be unloaded cleanly ... OK 
* checking whether the namespace can be loaded with stated dependencies ... OK 
* checking whether the namespace can be unloaded cleanly ... OK 
* checking loading without being on the library search path ... OK 
* checking dependencies in R code ... OK 
* checking S3 generic/method consistency ... OK 
* checking replacement functions ... OK 
* checking foreign function calls ... OK 
* checking R code for possible problems ... OK 
* checking Rd files ... OK 
* checking Rd metadata ... OK 
* checking Rd line widths ... OK 
* checking Rd cross-references ... OK 
* checking for missing documentation entries ... OK 
* checking for code/documentation mismatches ... OK 
* checking Rd \usage sections ... OK 
* checking Rd contents ... OK 
* checking for unstated dependencies in examples ... OK 
* checking examples ... NONE 
* DONE 

Status: OK 

R CMD check results 
0 errors | 0 warnings | 0 notes 
+0

感謝。確實。我還發現下面的可能性,刪除任何說明:把'setOldClass(c(「陌生人」))'在我的陌生人生成函數之前。必須等待賞金獎勵。 –

+0

@EricLecoutre很高興幫助! – duckmayr