Sunday, 15 September 2013

r - Does using package generics require the package to be in Depends or Imports? -


First of all, I have read a lot about this topic. I have studied conversations, and the problem is, though, I still feel that a particular subject has not been fully discussed. I am developing a package and I want to make my own method using autoplot () normal ggplot2 package, such as autoplot.MyFunction Such as a function () .

Depends in my current DESCRIPTION file: ggplot2 and everything works, there is no problem. I also combine it with Roxygen2 tag @import ggplot2 with the help file code, along with its code, @export . However

However most documents show that try to use one of the import: ggplot2 . The problem is that if I make this change, when I load my package with the library and try to use autoplot.MyFunction () , Then I have to face with the following error:

  & gt; Autoplot (tmp) error: The function "autoplot" could not be found  

Similarly, if I call the function directly ...

  & gt; Autoplot.MyFunction (tmp) error: The function "autoplot.MyFunction" could not be found  

However, if I use the :: method, then it works is .. .

  & gt; Ggplot2 :: autoplot (tmp)  

In my opinion, it is because import loads to ggplot2 Package (and therefore its function), but not attached , while dependent does attached

So, My question is simple, am I thinking that the package is correct in using generic, I should use the dependent: package , which means depends on my case: Ggplot2

of my package function To use the function from the package, I must be paired with import: package with :: , for example:

  Silly_fn & lt; - Function (data) {P < - ggplot (tmp, aes (x, y)) + geom_line () + geom_segment (aes (x = 0, xend = 20, y = 0, << Code>  

import requirement Will be: Grid :: and a Roxygen2 tag @import grid .

I think this is all right?

I am not a full expert on this, but here some works:

  # '@importFrom ggplot2 autoplot # '@export autoplot autoplot & lt; - autoplot #' @export "autoplot.foo" autoplot.foo "- function (obj, ...) {cat (" bar ")}  < / Pre> 

Unfortunately we need to explicitly export the method Because for some reason S3 dispatch for a reason from a package name space (Ase ggplot2 in this case) does not find the registered method of the package which does not import it, It does not mean that this base works for generics, so S3 is a subtlety of the registration mechanism that I do not understand.

Re: :: In your package or No, this is for debate if you have a @import or @importFrom There is no need to use, but some (including Headley) you should suggest to clarity. I personally advise against it because the R Profiler style does not resolve the function names of the package :: method , which makes the code harder to optimize (there are ways in which both Can meet the objectives).

Note that you must use @import and should not depend only on :: (you DESCRIPTION Need to update the file). Failure to do so will prevent me from becoming aware of R-dependence. I'm sure the rmd check yells about it.


No comments:

Post a Comment