Defining group common functions in an R package

I am having trouble defining the group generated in the R package I am writing.

Here is a pretty minimal example:

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))) 

If I paste this code into the R-terminal, everything works as expected:

 > 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) 

However, as soon as I try to compile this into a package, I encounter the following error:

 $ 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' 

I use the default output from package.skeleton () by adding:

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

to the NAMESPACE file

Any idea what I'm doing wrong?

+4
source share
1 answer

I can get this to work if I run the code at boot time. The key here is calling 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))) }) 

in the 'bla' package:

 > 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) 
+5
source

Source: https://habr.com/ru/post/1433564/


All Articles