R CMD check NOTE: Namespace in Imports field not imported - r

I continue to get the following R CMD check (via devtools::check()) for a package I am preparing to submit to CRAN (you can see the results for the package here:
Check: dependencies in R code
Result: NOTE
Namespace in Imports field not imported from: ‘tidyr’
All declared Imports should be used.
The source code for the package is here on GitHub. I've removed any mention of tidyr or its functions throughout the package, but the note remains. There are a number of Stack Overflow questions (i.e., this and other resources on this, but none seem to apply to this situation. How can I address this note?

This message appears when you include a package in the Imports: field in DESCRIPTION file and no function in this namespace is called by any function of the package. In this case, it means that in the R code of the package there was no call like tydir::fun, where fun represents any function of that package.
To solve it, simply delete the reference to the package in Imports: field within DESCRIPTION file. This was fixed in this commit for the package involved in the question: clustRcompaR.
If you don't want the message to appear when you check the package using devtools:check(), set option CRAN = FALSE: devtools::check(CRAN = FALSE)

Related

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

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:
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.

Conflicted package in R package building

I am creating my first package, here I am making some var estimations, the functions are running, however, I use packages that have the same function names.
Before writing the package I made an R script with the function and tests if it works, but at the top of my script I used the following code:
invisible(lapply(c("tibble","readxl","dplyr","stringr", "tidyr", "vars", "conflicted","forecast", "lubridate"), library, character.only = T))
conflict_prefer("select","dplyr")
conflict_prefer("lag", "dplyr")
conflict_prefer("filter", "dplyr")
The conflicted package chose the functions select, lag, and filter comes from the dplyr package rather from the stats package.
So I have not figured out how to use the conflict_prefer function inside the package.
Should they be the first lines of my function?
There is a roxygen way to prefer same-name functions?
I ask this because I get this warning:
> devtools::load_all()
i Loading FAVAR.MEF
Warning messages:
1: replacing previous import ‘dplyr::filter’ by ‘stats::filter’ when loading ‘FAVAR.MEF’
2: replacing previous import ‘dplyr::lag’ by ‘stats::lag’ when loading ‘FAVAR.MEF’
3: replacing previous import ‘stats::filter’ by ‘dplyr::filter’ when loading ‘FAVAR.MEF’
4: In setup_ns_exports(path, export_all, export_imports) :
Objects listed as exports, but not present in namespace: favar_est
Thanks in advance!!
If you are writing your own package and using external dependencies, you should not load them through repeated calls to library.
The proper way to do it is to state your dependencies in the DECRIPTION file of your package, which will mean that your dependencies are put on the search path in the correct order when your package is loaded. In your case, this removes the need for conflict_prefer, as dplyr will be higher up on the search path than stats. It also makes your package portable, because anyone who installs your package will have any missing dependencies installed automatically according to the packages listed in your DESCRIPTION file. Furthermore, doing it this way allows you to specify a minimum version of the dependency, so that anyone who already has an older version of the dependency installed will not come up against an obscure error when they try to use your package.
The DESCRIPTION file resides in the root directory of your package. It is a simple text file.
You need only add:
Depends:
tibble,
readxl,
dplyr,
stringr,
tidyr,
vars,
conflicted,
forecast,
lubridate
within this file, and your dependencies will be loaded with your package.

Extending a S4 class from another package: reconcilePropertiesAndPrototype error

I am trying to write a subclass for RJDBC::JDBCConnection as I need custom methods to connect the dbplyr package using the approach from dplyr#2941 (originally from here). However, I am not overwriting the *.JDBCConnection methods but want to write methods for a subclass of JDBCConnection.
Therefore, following the advise from this Stack Overflow question, I wrote my package which is essentially this:
### R/testclass.R ####################
#' Test class
#'
#' This extends JDBCConnection in package RJDBC
#'
#' #import RJDBC
#'
setClass("TestConnection", contains = "JDBCConnection")
### DESCRIPTION ######################
Package: test
Type: Package
Title: What the Package Does (Title Case)
Version: 0.1.0
Author: Who wrote it
Maintainer: The package maintainer <yourself#somewhere.net>
Description: More about what it does (maybe more than one line)
Use four spaces when indenting paragraphs within the Description.
License: What license is it under?
Encoding: UTF-8
LazyData: true
The class I want to extend exists, as can be checked with help("JDBCConnection-class", package = "RJDBC").
Calling devtools::document() within this packages returns the following error:
Updating test documentation
Loading test
Error in reconcilePropertiesAndPrototype(name, slots, prototype, superClasses, :
no definition was found for superclass "JDBCConnection" in the specification of class "TestConnection"
I also tried replacing #import with #importClassesFrom as per this SO question, but the result was the same.
How can I get document() to run?
You also need to add
Imports: RJDBC
to your DESCRIPTION file. See, for example, this guide:
If your package is using functions in other packages, you also need to
add some lines to your DESCRIPTION file.
...
Imports is used for packages that are needed by your package but that
don’t need to be loaded with library(). Packages referred to in
#import or #importFrom statements in your Roxygen2 comments, or whose
functions are accessed via the :: operator, should be here.
After that, your package document()'d fine for me.
I succeeded to document the package when I do not rely on roxygen2 to write my DESCRIPTION file but add the packages myself. NAMESPACE is managed by roxygen2.
If I add the Line
Imports: methods, RJDBC
or
Depends: RJDBC
to the DESCRIPTION file manually, devtools::document() runs without error.
[duckmayr found it out in the same time]

Using 'require' package code to obtain datapackages on the fly in R

I am writing an R package that uses a variety of bioconductor annotation data packages. The specific data packages vary with the use-case. As such, I have a function which does something like this:
if (!require(biocpack_name, character.only=T)) {
source("https://bioconductor.org/biocLite.R")
BiocInstaller::biocLite(biocpack_name)
require(biocpack_name , character.only=T)
}
biocpack_name can be several of ~30+ annotation data packages that are looked up based on the particular data being analysed. As such, I don't want to have to add each to 'Suggests' (Im not even sure that would work because the error is not for a package but rather a string specifying the package). R CMD CHK gives me this error:
'library' or 'require' call not declared from: ‘biocpack_name’
'library' or 'require' call to ‘biocpack_name’ in package code.
How do I get around this?
It's not an error, but a warning. It goes away if you use character.only = TRUE rather than T (I guess because the value of TRUE is known and cannot be re-assigned, but T is unknown and can be anything, including FALSE). But in addition follow the advice in the warning to use requireNamespace() (and not pollute the user search path); maybe db = get(biocpack_name, getNamespace(biocpack_name)) will allow you to use the annotation package the way you'd like, e.g., mapIds(db, ...).
If one were pedantic, adding the packages to the Enhances: field of the DESCRIPTION file would communicate that your package somehow works with the annotation packages, but does not result in installation of the package (e.g., for building the vignette) unless explicitly requested.

cover function (?) : how to deal with dependencies packages while developing new package in r

My the R package depends upon other package (for example "fields")
What is best practice to ensure that the package is loaded, when my package is loaded.
Should I write cover r program to do this ? Can or should such dependencies distributed with my distribution ?
I will appreciate a detail answer with scrips
Edit:
As per following suggestion I added the following in Discription file.
Depends: R (>= 1.8.0), fields
Still the fields package is not loaded automatically when I load my package.
This is something you specify in your DESCRIPTION file that you ship with your package. You can use either the 'Depends' field, or better is to use 'Imports' field in combination with a NAMESPACE file. Have a look at the DESCRIPTION and NAMESPACE files from some other packages, or read over the Writing R Extensions manual.

Resources