Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to declare a dependency on an R package from which you only use S3/S4 methods, but no exports?

Tags:

r

Currently I have in my package DESCRIPTION, a dependency on dbplyr:

Imports:
    dbplyr,
    dplyr

dbplyr is useful almost solely because of the S3 methods it defines: https://github.com/tidyverse/dbplyr/blob/main/NAMESPACE. The actual functions you call to use dbplyr are almost entirely from dplyr.

By putting dbplyr in my Imports, it should automatically get loaded, but not attached, which should be enough to register its S3 methods: https://r-pkgs.org/dependencies-mindset-background.html#sec-dependencies-attach-vs-load.

This seems to work fine, but whenever I R CMD check, it tells me:

N  checking dependencies in R code (10.8s)
   Namespace in Imports field not imported from: ‘dbplyr’
     All declared Imports should be used.

Firstly, why does R CMD check even check this, considering that it often makes sense to load packages without importing them. Secondly, how am I supposed to satisfy R CMD check without loading things into my namespace that I don't want or need?

like image 826
Migwell Avatar asked Sep 02 '25 11:09

Migwell


1 Answers

I am pretty sure two of your assumptions are false.

First, putting Imports: dbplyr into your DESCRIPTION file won't load it, so its methods won't be loaded from that alone. Basically the Imports field in the DESCRIPTION file just guarantees that dbplyr is available to be loaded when requested. If you import something via the NAMESPACE file, that will cause it to be loaded. If you evaluate dbplyr::something that will cause it to be loaded. Executing loadNamespace("dbplyr") is another way, and there are a few others. You may also load some other package that loads it.

Second, I think you have misinterpreted the error message. It isn't saying that you loaded it without importing it (though it would complain about that too), it is saying that it can't detect any use of it in your package, so maybe it shouldn't be a requirement for installing your package.

Unfortunately, the code to detect uses is fallible, so it sometimes misses uses. Examples I've heard about are:

  • if the package is only used in the default value for a function argument. This has been fixed in R-devel.
  • if the package is only used during the build to construct some object, e.g. code like someclass <- R6::R6Class( ... ) needs R6, but the check code won't see it because it looks at someclass, not at the source code that created it.
  • if the use of the package is hidden by specifying the name of the package in a character variable.
  • if the need for the package is indirect, e.g. you need to use ggplot2::geom_hex. That needs the hexbin package, but ggplot2 only declares it as "Suggested".

These examples come from this discussion: https://github.com/hadley/r-pkgs/issues/828#issuecomment-1421353457 .

The recommended workaround there is to create an object that refers to the imported package explicitly, e.g. putting the line

dummy_r6 <- function() R6::R6Class

into your package is enough to suppress the note without actually loading R6. (It will be loaded if you ever call this function.)

However, your requirement is stronger: you do need to make sure dbplyr is loaded if you want its methods to be used. I'd put something in your .onLoad() function that triggers the load. For example,

.onLoad <- function(lib, pkg) {
  # Make sure the dbplyr methods are loaded
  loadNamespace("dbplyr")
}

EDITED TO ADD: As pointed out in the comments, there's a bug in the check code that means it won't detect this as being a use of dbplyr. You really need to do both things, e.g.

.onLoad <- function(lib, pkg) {
  # Make sure the dbplyr methods are loaded
  loadNamespace("dbplyr")
  # Work around bug in code checking in R 4.2.2 for use of packages
  dummy <- function() dbplyr::across_apply_fns
}

The function used in the dummy construction is arbitrary; it probably doesn't even need to exist, but I chose one that does.

like image 70
user2554330 Avatar answered Sep 04 '25 00:09

user2554330