[R-pkg-devel] Define S4 method for generic in other package

Hadley Wickham h.wickham at gmail.com
Tue Sep 1 15:10:59 CEST 2015


Ah, the problem is because of roxyen2 because:

* To document your code, roxygen2 needs to load it first
* But it can't load it without the definition of git2r::pull

So you're stuck in a bit of a vicious circle. I usually break out of
this circle by temporarily patching the NAMESPACE by hand (in this
case with importFrom(git2r,pull) and then re-generating once I can get
the package loading.

It would be nice to have a more general solution, but I'm not sure how
to do it, because (e.g.) @export needs the loaded object so it can
generate the correct directives. Maybe I need a separate roclet that
only does import namespaces and can run by only inspecting the
comments, not by running the code. I filed an issue at
https://github.com/klutometis/roxygen/issues/372

Hadley

On Tue, Sep 1, 2015 at 3:53 AM, Thierry Onkelinx
<thierry.onkelinx at inbo.be> wrote:
> Dear Hadley,
>
> I've put a reproducible example at https://github.com/ThierryO/testS4
>
> Best regards,
> ir. Thierry Onkelinx
> Instituut voor natuur- en bosonderzoek / Research Institute for Nature
> and Forest
> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
> Kliniekstraat 25
> 1070 Anderlecht
> Belgium
>
> To call in the statistician after the experiment is done may be no
> more than asking him to perform a post-mortem examination: he may be
> able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
> The plural of anecdote is not data. ~ Roger Brinner
> The combination of some data and an aching desire for an answer does
> not ensure that a reasonable answer can be extracted from a given body
> of data. ~ John Tukey
>
>
> 2015-09-01 0:32 GMT+02:00 Hadley Wickham <h.wickham at gmail.com>:
>> I'd recommend creating a minimal package that illustrates the problem
>> using an existing S4 generic in a base package (e.g. stats4::AIC).
>> That makes it easier to figure out what's gone wrong.
>>
>> It's unlikely to be a roxygen2 problem since the translation from
>> @importFrom foo bar (etc) to importFrom(foo, bar) is quite
>> straightforward.
>>
>> Hadley
>>
>> On Fri, Aug 28, 2015 at 3:23 AM, Thierry Onkelinx
>> <thierry.onkelinx at inbo.be> wrote:
>>> Dear all,
>>>
>>> I'm trying to define a new S4 method for a generics defined in an
>>> other package. Here is my attempt, but that isn't working.
>>>
>>> bar <- function(a, b) is defined as generic and S4 method in the package foo
>>>
>>> #' @importFrom foo bar
>>> #' @importMethodsFrom foo bar
>>> setMethod(
>>>   f = "bar",
>>>   signature = signature(A = "myClass"),
>>>   definition = function(
>>>     a,
>>>     b
>>>   ){
>>>     bar(
>>>       a = myClass at A,
>>>       b = myClass at B
>>>     )
>>>   }
>>> )
>>>
>>> When I build my package I get the error "no existing definition for
>>> function 'bar'".
>>>
>>> Any suggestions?
>>>
>>> Best regards,
>>>
>>> ir. Thierry Onkelinx
>>> Instituut voor natuur- en bosonderzoek / Research Institute for Nature
>>> and Forest
>>> team Biometrie & Kwaliteitszorg / team Biometrics & Quality Assurance
>>> Kliniekstraat 25
>>> 1070 Anderlecht
>>> Belgium
>>>
>>> To call in the statistician after the experiment is done may be no
>>> more than asking him to perform a post-mortem examination: he may be
>>> able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher
>>> The plural of anecdote is not data. ~ Roger Brinner
>>> The combination of some data and an aching desire for an answer does
>>> not ensure that a reasonable answer can be extracted from a given body
>>> of data. ~ John Tukey
>>>
>>> ______________________________________________
>>> R-package-devel at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>>
>>
>>
>> --
>> http://had.co.nz/



-- 
http://had.co.nz/



More information about the R-package-devel mailing list