R: cannot find exported function even after importing entire namespace - r

I'm building a package, myPackage that imports from several other packages. The problem is that a function fun (it's actually a method) that is exported from another package otherPackage cannot be found when run. I did all of the following:
Imports: otherPackage and Depends: methods in myPackage's DESCRIPTION file
import(otherPackage) in myPackage's NAMESPACE file
Also tried adding importMethodsFrom(otherPackage,"fun")
The only way I found it to work is if I replace Imports: otherPackage with Depends: otherPackage, but we all know that it is recommended to import another package's namespace instead of attaching it to the search path. Can someone please explain why Importing it doesn't work?
Specifics: the function in question is keys from package AnnotationDbi in Bioconductor. The specific error is Error in .select(x, keys, columns, keytype) :
could not find function "keys"
Thank you.

Related

import all the functions of a package except one when building a package

I'm building an R package (mypackage) that imports data.table and another package (let's call it myotherpackage).
Imports: data.table, myotherpackage is in the DESCRIPTION file of mypackage.
myotherpackage imports dplyr, which has several functions named like the data.table functions, so I get warnings like this everytime I load mypackage:
Warning: replacing previous import ‘data.table::first’ by ‘dplyr::first’ when loading ‘mypackage’
Is there a way to import all the functions of data.table except "first" for example? I'd then use data.table::first in the code if I need to use it.
Or is there a better way to handle it? I'm trying to avoid the warning every time someones imports the package. Thank you!
The NAMESPACE file is somewhat flexible here, as described in Writing R Extensions.
The two main import directives are:
import(PACKAGE)
which imports all objects in the namespace into your package. The second option is to do specific imports using:
importFrom(PACKAGE, foo)
which gives you access to foo() without needing the fully qualified reference PACKAGE::foo().
But these aren't the only two options. You can also use the except argument to exclude just a handful of imports:
import(PACKAGE, except=c(foo,bar))
which gives you everything from PACKAGE's namespace but foo() and bar(). This is useful - as in your case - for avoiding conflicts.
For roxygen, great catch on figuring out that you can do:
#' #rawNamespace import(PACKAGE, except = foo)
to pass a raw NAMESPACE directive through roxygen.

R CMD check NOTE: Namespace in Imports field not imported

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)

Use dependencies in R packages through library() / Description file

I'm writing an R package that has several dependencies of other packages, some of them are available in CRAN and other ones are homemade.
According to the help, library("my_package") will load the namespace of the package once I have previously installed it, i.e, install.package("my_package").
Nevertheless, once I have installed the package I am able to use all the functions of the installed but not loaded package through my_package::my_function(), so if my package has dependencies, beside adding those into DESCRIPTION file:
Imports:
dplyr,
my_package2,
ggvis,
in the root of the package folder.
Do I have to load the dependencies of the new package through library() or the final user will see an error if he has not installed on his computer as the required packages are specified in the Imports section?
No, the user does not have to load the packages that are used by functions in my_package.
The fact that you have listed a package under Imports: in the DESCRIPTION file means that during the installation of my_package, R will check that this package is available on your system. This means then that functions in my_package can use functions from these packages using the :: notation, as you suggested.
Using the :: notation is the recommended way to refer to functions from other packages, but there are also other options:
In order to make all the functions from, say, dplyr accessable without :: in my_package, you could add import(dplyr) to the NAMESPACE file. This is convenient, if you use many functions from a package.
If you intend to use only, say, the function select from dplyr, you could add importFrom(select, dplyr) to the NAMESPACE file.
You could also add the package to the DESCRIPTION file under Depends:. This would mean that the package is loaded to the global environment when you use library(my_package). This is almost never a good solution.
The general idea of dependencies is R is that my_package will have "it's own version" of the packages it depends on loaded. Therefore, you can always be sure that you will, e.g., use the function select() from the dplyr package, as you intended to do. The exception is the use of Depends: which bypasses this system. In this case, my_package will look for functions in the global environment and if somebody should have defined some function called select() in the global environment, my_package will use this function and you will get unexpected results.
Example 1:
DESCRIPTION file:
Imports:
dpylr
some function from my_package:
my_fun <- function(...) {
dplyr::mutate(...) %>%
dplyr::select(1:3)
}
Example 2:
DESCRIPTION file:
Imports:
dpylr
NAMESPACE file:
import(dplyr)
some function from my_package:
my_fun <- function(...) {
mutate(...) %>%
select(1:3)
}
Example 3:
DESCRIPTION file:
Imports:
dpylr
NAMESPACE file:
importFrom(dplyr,select)
some function from my_package:
my_fun <- function(...) {
dpylr::mutate(...) %>%
select(1:3)
}
You find more detailed explanations of how to handle dependencies in R packages on the web. For instance the following are useful:
On the DESCRIPTION file
On the NAMESPACE file
Also, it is not necessary to write the NAMESPACE file by hand. You can let roxygen2 do that for you. Read the documentation for more information.

How to handle dependencies (`Depends:`) of imported packages (`Imports:`)

I'm trying to use Imports: instead of Depends: in the DESCRIPTION files of my packages, yet I still feel I've got some more to understand on this ;-)
What I learned from this post (by the way: awesome post!!!) is that everything my package, say mypkg, imports (say imported.pkg) via Imports: lives in environment imports:mypkg instead of being attached to the search path. When trying to find foo that ships with imported.pkg, R looks in imports:mypkg before traversing the search list. So far, so good.
Actual question
If imported.pkg (imported by mypkg) depends on a certain other package (stated in Depends: section of the package's DESCRIPTION file), do I need to make this very package a Depends: dependency of my package in order for R to find functions of that package? So it seems to me at the moment as otherwise R complains.
Evidence
Seems like simply importing such a package is not enough. As an example, take package roxygen2 (CRAN). It depends on digest while importing a bunch of other packages. I imported it (along with digest as mypkg also needs it) and checked environment imports:mypkg which does list the digest function: "digest" %in% parent.env(asNamespace("mypkg")) returns TRUE
Yet when running roxygenize() from within a function that is part of mypkg, R complains that it can't find digest.
You could have a look at my blog : http://r2d2.quartzbio.com/posts/package-depends-dirty-hack-solution.html
Now i have a better and cleaner solution but not published yet.
Hope it helps.

Object not found after installing and loading package

I've thrown together a bunch of my utility functions into a package. However, I can't seem to access them after I've installed the package. I get errors of the form Error: object 'function_name' not found
Building the package, there are no error messages
Installing the package from source, there are no error messages
Loading the package, there are no error messages (library() nor require())
The package documentation is accessible once loaded
I'm using roxygen2 to generate documentation and the namespace
Any thoughts?
Do you use a NAMESPACE and forgot to add the object in question?
If you're using roxygen2, have you remembered to add #' #export function_name to the functions you want included in the namespace?
If the function name is not exported, you may need to use ":::"
pkgname:::function_name
I believe that CRAN now requires a NAMESPACE, and I think R 2.14.x may even require them.
The same problem for me, you need to change NAMESPACE file. sometime NAMESPACE's content looks like this:
# Generated by roxygen2: do not edit by hand
But you need to change it by hand, like this:
# Generated by roxygen2: do not edit by hand
export("function_name1", "function_name2")
OR use exportPattern("^[^\\.]") to export all function.

Resources