Viechtbauer Wolfgang (STAT)
2014-Sep-28 12:47 UTC
[Rd] Using requireNamespace() instead of require()
Suppose I have a function funcA() in package pkgA that requires function funcB() from another package pkgB under certain circumstances. Those circumstances are rare though, so I put package pkgB under 'Suggests', and use this in funcA() when those circumstances do arise: if (require("pkgB", quietly=TRUE)) { pkgB::funcB(...) } else { stop("Please install package 'pkgB' to do this.") } I understand that using requireNamespace() would be preferable and I would also prefer to do that. But funcB() depends on funcC() from pkgC that it lists under 'Depends'. So, with require("pkgB"), both namespaces (of pkgB and pkgC) are loaded and attached to the search list and things run. But with requireNamespace("pkgB"), only the namespace of pkgB is loaded, so funcC() can't be found. So, I figured, I also use requireNamespace("pkgC"), but that's not sufficient, since funcB() calls funcC() and not pkgC::funcC(). Is there any solution to this? Best, Wolfgang
On 28/09/2014, 8:47 AM, Viechtbauer Wolfgang (STAT) wrote:> Suppose I have a function funcA() in package pkgA that requires function funcB() from another package pkgB under certain circumstances. Those circumstances are rare though, so I put package pkgB under 'Suggests', and use this in funcA() when those circumstances do arise: > > if (require("pkgB", quietly=TRUE)) { > pkgB::funcB(...) > } else { > stop("Please install package 'pkgB' to do this.") > } > > I understand that using requireNamespace() would be preferable and I would also prefer to do that. But funcB() depends on funcC() from pkgC that it lists under 'Depends'. So, with require("pkgB"), both namespaces (of pkgB and pkgC) are loaded and attached to the search list and things run. But with requireNamespace("pkgB"), only the namespace of pkgB is loaded, so funcC() can't be found. So, I figured, I also use requireNamespace("pkgC"), but that's not sufficient, since funcB() calls funcC() and not pkgC::funcC(). > > Is there any solution to this?The solution needs to be implemented by the maintainer of pkgB. It probably doesn't really need pkgC on the search list, and could be modified to import funcC or explicitly call pkgC::funcC. If you aren't the maintainer of pkgB, and the real maintainer is unresponsive, then you're stuck using require(). Duncan Murdoch
Apparently Analagous Threads
- "False" warning on "replacing previous import" when re-exporting identical object
- S4 NAMESPACE method imports and exports do not include (promoted?) generics
- [LLVMdev] [Help] How can we call an object's virtual function inside IR?
- RFC: Dynamically Allocated "Callee Saved Registers" Lists
- R-3.6.2 make check fails