UseMethod
Rが探しているもの(つまり、クラスMyClass:MyGeneric.MyClassのxで呼び出される関数MyGeneric(x))を見つけたら、Rがメソッドを見つける方法を理解しようとしています。具体的には、どの環境が関係していますか?
検索メカニズムを指定していないR言語マニュアルの「5.3メソッドディスパッチ」と「5.4UseMethod」のセクションを読みました。のR-ヘルプページにUseMethod
手がかりがあります。
...UseMethod and NextMethod search for methods in two places:
first in the environment in which the generic function is called,
and then in the registration data base for the environment
in which the generic is defined (typically a namespace)
しかし、これは合計されません(私の頭の中=)。具体的な例を次に示します。
library( xts )
as.matrix # shows UseMethod("as.matrix")
methods("as.matrix") # shows as.matrix.xts. * indicates non-visible
showMethods("as.matrix") # says <not an S4 generic function>
data(sample_matrix)
as.matrix( as.xts(sample_matrix) ) # how does R find as.matrix.xts?? its not exported!
as.matrix
で定義されていnamespace:base
ます。Rがその環境、または呼び出し元の環境(R_GlobalEnv)を使用する場合、as.matrix.xts
エクスポートされていないため、Rは見つかりませんでした。xts内の関数が呼び出し環境にあるas.matrix
ためas.matrix.xts
、呼び出し環境が機能しているように見えます。私は何が欠けていますか?