Import all but one (or a few) function in a package
torfason opened this issue · 5 comments
I love the import package when it comes to pulling a single function from sometimes very large packages without polluting the namespace.
However, a pretty common use case (which i'm not the only one facing) is that I'm using a package I like, but it happens to have one function that conflicts with a very important other function.
So, what I would love to have is something like:
import::from_excluding(nicepackage,conflictingfunction)
Workarounds exist of course, including creating elaborate orderings of package import statements, but all solutions I've found, including those presented in the link above, seem very inferior to the solution I propose, which I'm kind of hoping would not be a big extension to the import package.
Issue #25 reflects the desire to import all objects from a particular package. It seems like the use case described there is similar apart from the fact of using import::here()
instead of import::from()
. It seems like a good idea that an implementation resolving this issue would:
- Allow either all objects to be imported from a package, or all except a few
- Work regardless of whether one uses
here()
orfrom()
for the import
One reasonable way to resolve both #23 would be to use getNamespaceExports() to choose all objects in a package to import. The "all but one" use case, could then be implemented by using a set diff to remove the specified objects.
The main barrier to just creating the list programmatically outside the package seems to be that import::from() does not accept character vectors for the object list, and does checking that prevents one from just calling it with a programmatic argument list using do.call().
On further inspection, it now seems to me that the .character_only parameter available in the development version allows closing #23, #13, and #8. Example of how the parameter solves this particular issue follows, demonstrating how one would go about importing all the objects from dplyr
except filter()
and mutate()
:
objects=setdiff(getNamespaceExports("dplyr"), c("filter", "mutate"))
import::from("dplyr", objects, .character_only=TRUE)
head(select(cars,dist))
#> dist
#> 1 2
#> 2 10
#> 3 4
#> 4 22
#> 5 16
#> 6 10
# mutate() fails (like we wanted) because we excluded the mutate function
mutate(cars,speed=10)
#> Error in mutate(cars, speed = 10): could not find function "mutate"
Created on 2020-06-25 by the reprex package (v0.3.0)
This has now been fixed on master. If you could install the most current version and test whether the fix works for you (and if the new version works for you in general) that would be greatly appreciated. You can get the fix with:
remotes::install_github("smbache/import")
To import all or all-but-a-few functions, one can either use the character_only
parameter or the newer .all
and .except
parameters:
# Import all functions from psych
import::from(psych, .all=TRUE)
# Using .except implies .all=TRUE by default
import::from(dplyr, .except="filter")
Fixed in release 1.2.0