2015-01-11 25 views
7

我已經使用S4類編寫了一個包,並且希望使用函數rbind,cbind和這些定義的類。在包中使用cbind,rbind和s4類的正確方法

因爲它似乎沒有可以定義rbindcbind直接作爲S4的方法我定義rbind2cbind2代替:

setMethod("rbind2", signature(x="ClassA", y = "ANY"), 
    function(x, y) { 
     # Do stuff ... 
}) 

setMethod("cbind2", signature(x="ClassA", y = "ANY"), 
    function(x, y) { 
     # Do stuff ... 
}) 

?cbind2我瞭解到,這些功能都需要使用methods:::bind_activation,以取代被激活從基地開始綁定和綁定。

我包括使用.onLoad功能包中的文件R/zzz.R電話:

.onLoad <- function(...) { 
    # Bind activation of cbind(2) and rbind(2) for S4 classes 
    methods:::bind_activation(TRUE) 
} 

可正常工作。然而,運行v CMD檢查,因爲我使用的方法的未導出的功能,我現在得到以下注意事項:

* checking dependencies in R code ... NOTE 
Unexported object imported by a ':::' call: 'methods:::bind_activation' 
    See the note in ?`:::` about the use of this operator. 

我怎樣才能擺脫注意的,什麼是定義方法cbind的正確方法和在一個軟件包中安裝S4課程?

+0

你想介紹一下你試圖添加'rbind'和'cbind'方法的幾個S4類的類定義(例如'setClass(「ClassA」,...)')嗎?這將使您更容易爲您的問題找出解決方案。 – nrussell

+1

在這種情況下,類定義應該不重要,因爲它只是方法選擇/調度的問題。所以你可以使用任何定義像setClass(「ClassA」,表示(a =「數字」))。 – user625626

+0

另外,你能否解釋爲什麼「* ...它似乎不可能直接定義rbind和cbind作爲S4方法...... *」 - 也許你試圖實現這個代碼? – nrussell

回答

4

我認爲基本上矩陣包中的cBind幫助頁面歷史上是準確的,但不是最近。下面是一類

.A = setClass("A", representation(x="numeric")) 

有沒有通用的,所以要創建一個,調度的 '...' 的說法(見?setMethod?dotsMethods

getGeneric("cbind") 
## NULL 
setGeneric("cbind", signature="...") 
## Creating a new generic function for 'cbind' in the global environment 

然後實現的方法

setMethod("cbind", "A", function(..., deparse.level=1) "cbind,A-method") 
## [1] "cbind" 

最後使用它

> cbind(.A(), .A()) 
[1] "cbind,A-method" 

只要'...'參數是相同的(可能派生的)類,這通常是很好的。

> cbind(.A(), integer()) 
    [,1] 
[1,] ? 

我相信bind_activation()具有全球性的影響,不只是在你的包調度;應該避免它(例如,它不再用於Matrix包中)。

另外,我覺得,這已在R-devel的更新


r67699 |勞倫斯| 2015-02-01 10:13:23 -0800(Sun,2015年2月1日)| 4 lines

cbind/rbind現在至少遞歸地委託給cbind2(rbind2) 一個參數是一個S4對象並且S3派遣失敗;在* bind函數中的S3 dispatch期間也考慮S4 繼承。