Confused about NAMESPACE

I'm confused about the purpose of NAMESPACE. Whenever I import from package, I put @importFrom package function_from_package. However, when I run devtools::check(), I always get this message:

Namespace dependencies missing from DESCRIPTION Imports/Depends entries:

Does this mean that even if I use the @importFrom tag, do I always need to add the package to my Imports/Depends in DESCRIPTION? If I'm going to need to add this package to my DESCRIPTION no matter what, why do we need to bother with NAMESPACE in the first place?

I think the best answer is to quote from r-pkgs:

Let’s make this crystal clear:

Listing a package in Imports in DESCRIPTION does not “import” that package.

It is natural to assume that listing a package in Imports actually “imports” the package, but this is just an unfortunate choice of name for the Imports field. The Imports field makes sure that the packages listed there are installed when your package is installed. It does not make those functions available to you, e.g. below R/, or to your user.

It is neither automatic nor necessarily advisable that a package listed in Imports also appears in NAMESPACE via imports() or importFrom(). It is common for a package to be listed in Imports in DESCRIPTION, but not in NAMESPACE. The converse is not true. Every package mentioned in NAMESPACE must also be present in the Imports or Depends fields.

So the DESCRIPTION listing is about what's needed to use your package, and will be used by R to find dependencies during installation; the NAMESPACE listing is about finding functions in the dependencies when your package is loaded/used.

And in case it's not clear: @importFrom actually has nothing to do with DESCRIPTION, it's an instruction to Roxygen to automatically add this function to NAMESPACE. To add to DESCRIPTION, the "Posit-endorsed" way is with {usethis}.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.