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?
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:
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.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With