From wolfgang.viechtbauer at maastrichtuniversity.nl  Sun Sep 28 14:47:15 2014
From: wolfgang.viechtbauer at maastrichtuniversity.nl (Viechtbauer Wolfgang (STAT))
Date: Sun, 28 Sep 2014 14:47:15 +0200
Subject: [Rd] Using requireNamespace() instead of require()
Message-ID: <077E31A57DA26E46AB0D493C9966AC730DCCF99AE3@UM-MAIL4112.unimaas.nl>

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



[Rd] Using requireNamespace() instead of require()
Viechtbauer Wolfgang (STAT