First of all, I have read around this topic a lot. I have studied the conversations here, here, here and here. The problem is, however, I still think one particular topic isn't discussed fully. I am developing a package and I wish to create my own method using the autoplot() generic from the ggplot2 package, i.e. have a function such as autoplot.MyFunction().
My current DESCRIPTION file contains Depends: ggplot2 and everything works, no problem. I also combine this with Roxygen2 tags @import ggplot2 within my function's help file code, coupled with @export.
However most documentation describes how one should try to use Imports: ggplot2. The trouble is, if I make this change, when I load my package with library(my_package) and try to use autoplot.MyFunction(), I am faced with the following error:
> autoplot(tmp)
Error: could not find function "autoplot"
Similarly, if I call the function directly...
> autoplot.MyFunction(tmp)
Error: could not find function "autoplot.MyFunction"
However if I use the :: method, then it does work...
> ggplot2::autoplot(tmp)
From my understanding, this is because Imports loads the ggplot2 package (and therefore its functions), but doesn't attach it, whereas Depends does attach it.
So, finally, my question is simple, am I correct in thinking that to use package generics, I should be using Depends: package, i.e. in my case Depends: ggplot2.
Then for using functions from packages within my package functions I should be using Imports: package coupled with ::, e.g.:
silly_fn <- function (data) {
p <- ggplot(tmp, aes(x, y)) +
geom_line() +
geom_segment(aes(x = 0, xend = 20, y = 0, yend = 20),
arrow = grid::arrow(length = grid::unit(0.15, "inches")))
p
}
would require Imports: grid and a Roxygen2 tag @import grid.
I think this is all correct?