0

I have a revision of a package in which I have added some plotting code that uses the ggplot2 package as well as a custom scale from the package scales. In particular, the new code has one line that references the scales package, of the form:

trans = scales::trans_new("new scale", ...)

I sent the package to Winbuilder to check it and I get this error:

Error in loadNamespace(name) : there is no package called 'scales'

... and indeed I get the same error if I run R CMD check --as-cran

  • Yes, scales is installed on my system.

  • Yes, scales is included in Suggests in my DESCRIPTION file; I have double- and triple-checked this.

  • Yes, scales appears to be a package still available on CRAN, with lots and lots of reverse dependencies.

I also have dozens of calls to ggplot2::xxxx routines in my code, and ggplot2 is also in Suggests. No errors there.

What could I be doing wrong?

Russ Lenth
  • 5,922
  • 2
  • 13
  • 21
  • ggplot2 is not in Depends, and I use `ggplot2::` a bunch of times in the same way. I definitely do not want extra stuff in Depends. – Russ Lenth Apr 16 '19 at 00:34
  • If you have dozens of calls to something then your package clearly depends on it. What would happen to your users when they hit those references? – Elin Apr 16 '19 at 01:18
  • I have a `requireNamespace()` check accompanying them. I figured it out. See my answer... – Russ Lenth Apr 16 '19 at 01:22
  • @Elin Thinking about this further, I'd say that one shouldn't put anything in Depends unless the user really needs that package in the search path. For packages needed to accomplish the central purposes of a package, those should go in Imports. In this case, users of my package may never use any of its plotting capabilities, so I put **ggplot2** in Suggests and do the namespace check. I think excessive dependencies is a real issue with R packages. Many have hundreds of dependencies, and that's all stuff that needs to be loaded into memory. – Russ Lenth Apr 16 '19 at 15:31

1 Answers1

0

I figured this out; it was a bit subtle (at least for somebody like me).

In the other places in my package where I call functions from a package in Suggests, I also have a line of code that looks like this:

if (!requireNamespace("ggplot2", quietly = TRUE))
    stop ("The 'ggplot2' package is not installed installed.", call. = FALSE)
... code that calls ggplot2:: functions

The problem here was that (a) I didn't have such a check for scales; but more important, (b) the assignment above, trans = scales::... was in the namespace of my package.

So what I did was move the assignment of trans to the interior of the function where it is used, and preceded it with a requireNamespace() test similar to the above. Now my package check at least passes that point in the --as-cran check.

Russ Lenth
  • 5,922
  • 2
  • 13
  • 21