Namespace dependencies not required
If you use import
or importFrom
in your NAMESPACE file, you should have an entry for that package in the Imports section of your DESCRIPTION file (unless there is a reason that you need to use Depends in which case the package should have an entry in Depends, and not Imports)
Here is a relevant section of Writing R Extensions
The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached. Namespaces accessed by the ‘::’ and ‘:::’ operators must be listed here, or in ‘Suggests’ or ‘Enhances’ (see below). Ideally this field will include all the standard packages that are used, and it is important to include S4-using packages (as their class definitions can change and the DESCRIPTION file is used to decide which packages to re-install when this happens). Packages declared in the ‘Depends’ field should not also be in the ‘Imports’ field.
I made a package with a single function f
. I made a NAMESPACE file with the same importFrom
line that you say you have in yours.
NAMESPACE file
export("f")
importFrom("ggplot2","ggplot","geom_histogram")
At this point, if I run R CMD check
, as expected, I get an error:
Namespace dependency not required: ‘ggplot2’
But, if I add Imports: ggplot2
to the DESCRIPTION such that my DESCRIPTION file is as follows, it passes R CMD check
with no problems.
DESCRIPTION file
Package: anRpackage
Type: Package
Title: What the package does (short line)
Version: 1.0
Date: 2012-11-07
Author: Me
Maintainer: Who to complain to <yourfault@somewhere.net>
Description: More about what it does (maybe more than one line)
License: GPL
Imports: ggplot2
Namespace dependencies not required error although I've used Imports in DESCRIPTION
When R CMD check runs, it built a binary package .zip file on the parent folder of the package's folder, and also a folder call pkgname.check. I think the next time R CMD check runs, it may not rebuild that folder or that file, depending on whether changes have been made in the package. I deleted those file and folder and rebuild, everything works.
Mysterious Namespace dependency not required: ‘rlang’
It turns out that the devtools::check() error was a ghost somewhere in a sqlpetr.Rcheck folder that it creates next door to the package directory. Once that error message had been issued it would not go away, even after I corrected the problem. Once I deleted that directory, the error message disappeared.
import NAMESPACE and DESCRIPTION question?
I would suggest reading the Namespaces chapter of Hadley's R Packages book. But in short, the answer is No.
Are imported functions in the NAMESPACE file attached to the R session when the main package is attached?
No, they are not. Imported functions are available for use in the package internals, but not attached to the user's search tree.
Another source for info is, of course, Writing R Extensions. They describe IMPORTS as:
The ‘Imports’ field lists packages whose namespaces are imported from (as specified in the NAMESPACE file) but which do not need to be attached.
As a demonstration, the current version of ggplot2
, v 3.2.1, has import(scales)
in its NAMESPACE file. In a fresh R session, we can load ggplot2
and observe that the scales
package is not attached:
library(ggplot2)
percent(1)
# Error in percent(1) : could not find function "percent"
scales::percent(1)
# [1] "100%"
ggplot2
uses functions from scales
internally, and can do so without using the package::function
notation. This is what the import(scales)
accomplishes. However, unlike with Depends
, scales
is not attached for the user.
Related Topics
Remove Rows Where All Variables Are Na Using Dplyr
Extract Rgb Channels from a Jpeg Image in R
How to Get Factor Matrices in R
Applying R Script Prepared for Single File to Multiple Files in the Directory
How to Automate Multiple Requests to a Web Search Form Using R
R Print Equation of Linear Regression on the Plot Itself
Why Is Expand.Grid Faster Than Data.Table 's Cj
Multiple Histograms with Ggplot2 - Position
How to Remove Na from Facet_Wrap in Ggplot2
Print String and Variable Contents on the Same Line in R
Generating a Vector of Difference Between Two Vectors
Caching the Mean of a Vector in R