Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export S3 method for a 'function' class object

Function objects seems to work well with dispatching of S3 methods.
But for some reason they cannot be exported in NAMESPACE file.

Below code works with dispatching to *.function method:

as.abc = function(x, ...){
    UseMethod("as.abc")
}
as.abc.list = function(x, ...){
    stopifnot(is.list(x))
    structure(x, class="abc")
}
as.abc.function = function(x, ...){
    stopifnot(is.function(x))
    structure(x, class="abc")
}
# list
l = as.abc(list(1))
str(l)
#List of 1
# $ : num 1
# - attr(*, "class")= chr "abc"

# function
f = as.abc(function(x) x)
str(f)
#function (x)  
# - attr(*, "srcref")=Class 'srcref'  atomic [1:8] 1 12 1 24 12 24 1 1
# .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x3dbb848> 
# - attr(*, "class")= chr "abc"

But when I try to define export in NAMESPACE as:

export(as.abc)
S3method(as.abc, list)
S3method(as.abc, function)

It throws an error while R CMD check:

* checking package namespace information ... ERROR
Invalid NAMESPACE file, parsing gives:
Error in parse(nsFile, keep.source = FALSE, srcfile = NULL): 29:26: unexpected ')'
28: S3method(as.abc, list)
29: S3method(as.abc, function)

I tried also wrap into ` but it didn't help too

S3method(as.abc, `function`)

What is the proper way to export method for a function class?

like image 932
jangorecki Avatar asked Apr 26 '26 06:04

jangorecki


1 Answers

According to Writing R Extensions section 1.5.2, you have to use regular quotes when registering S3 methods that work on objects of class function:

(Note that function and class names may be quoted, and reserved words and non-standard names such as [<- and function must be.)

like image 82
Chris Avatar answered Apr 29 '26 04:04

Chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!