4

作成しているRパッケージ内でジェネリックグループを定義するのに問題があります。

これはかなり最小限の例です:

setGroupGeneric('FooBarFunctions', function(x, y) NULL)

setGeneric('foo', group = 'FooBarFunctions', function(x, y) standardGeneric('foo'))
setGeneric('bar', group = 'FooBarFunctions', function(x, y) standardGeneric('bar'))

setMethod('foo', signature(x = 'ANY', y = 'ANY'),
function(x, y)
  cat(sprintf('foo,ANY (%s),ANY (%s)\n', x, y)))

setMethod('bar', signature(x = 'ANY', y = 'ANY'),
function(x, y)
  cat(sprintf('bar,ANY (%s),ANY (%s)\n', x, y)))

setMethod('FooBarFunctions', signature(x = 'character', y = 'ANY'),
function(x, y)
  cat(sprintf('FooBarFunctions,character (%s),ANY (%s)\n', x, y)))

このコードをRターミナルに貼り付けると、すべてが期待どおりに機能します。

> foo(1, 2)
foo,ANY (1),ANY (2)
> bar(1, 2)
bar,ANY (1),ANY (2)
> foo('a', 2)
FooBarFunctions,character (a),ANY (2)
> bar('a', 2)
FooBarFunctions,character (a),ANY (2)

ただし、これをパッケージにビルドしようとすると、次のエラーが発生します。

$ R CMD INSTALL .
* installing to library ‘~/R/x86_64-pc-linux-gnu-library/2.15’
* installing *source* package ‘anRpackage’ ...
** R
** preparing package for lazy loading
** help
No man pages found in package  ‘anRpackage’ 
*** installing help indices
** building package indices
** testing if installed package can be loaded
**Error in .setupMethodsTables(generic) : 
  trying to get slot "group" from an object of a basic class ("NULL") with no slots**
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘~/R/x86_64-pc-linux-gnu-library/2.15/anRpackage’

package.skeleton()からのデフォルトの出力を使用しており、以下を追加しています。

exportPattern("^[[:alpha:]]+")

NAMESPACEファイルに

私が間違っていることについて何か考えはありますか?

4

1 に答える 1

5

ロード時にコードを実行すると、これを機能させることができます。ここで重要なのはevalqOnLoad電話です

evalqOnLoad({

    setGroupGeneric('FooBarFunctions', function(x, y) NULL)

    setGeneric('foo', group = 'FooBarFunctions', function(x, y) standardGeneric('foo'))
    setGeneric('bar', group = 'FooBarFunctions', function(x, y) standardGeneric('bar'))

    setMethod('foo', signature(x = 'ANY', y = 'ANY'),
    function(x, y)
      cat(sprintf('foo,ANY (%s),ANY (%s)\n', x, y)))

    setMethod('bar', signature(x = 'ANY', y = 'ANY'),
    function(x, y)
      cat(sprintf('bar,ANY (%s),ANY (%s)\n', x, y)))

    setMethod('FooBarFunctions', signature(x = 'character', y = 'ANY'),
    function(x, y)
      cat(sprintf('FooBarFunctions,character (%s),ANY (%s)\n', x, y)))

})

パッケージ内'bla':

> require( bla )
Le chargement a nécessité le package : bla
> foo(1, 2 )
foo,ANY (1),ANY (2)
> bar(1, 2 )
bar,ANY (1),ANY (2)
> foo("a", 2 )
FooBarFunctions,character (a),ANY (2)
> bar("a", 2 )
FooBarFunctions,character (a),ANY (2)
于 2012-10-30T10:49:06.203 に答える