From simon.urbanek at r-project.org  Sat Feb 23 05:12:43 2013
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Fri, 22 Feb 2013 23:12:43 -0500
Subject: [Rd] Recommended way to call/import functions from a Suggested
	package
In-Reply-To: <CABdHhvHkJkyzKODdDKKt+fmCeUAXJC7L24vXLuLPWmRYsYZtkQ@mail.gmail.com>
References: <FD2C7845-F6AD-4AA1-91C1-8CB14F43AA7D@stat.ubc.ca>
	<CABdHhvHkJkyzKODdDKKt+fmCeUAXJC7L24vXLuLPWmRYsYZtkQ@mail.gmail.com>
Message-ID: <FDF2030A-2A91-4A40-B09F-C4DD78682A62@r-project.org>


On Feb 22, 2013, at 9:13 PM, Hadley Wickham wrote:

> Hi Davor,
> 
> To the best of my knowledge, there's only one way to use functions
> from a suggested package: with require:
> 
> if (require("suggested_package")) {
>  function_from_suggested_package()
> } else {
>  stop("suggested package not installed")
> }
> 
> Unfortunately I don't think there's any way to use a suggested package
> without polluting the search path.
> 

Why -- wouldn't

if (is.function(try(foo::bar, silent=TRUE))) {
  foo::bar(...)
}

do the job?



> Hadley
> 
> On Fri, Feb 22, 2013 at 6:26 PM, Davor Cubranic <cubranic at stat.ubc.ca> wrote:
>> If in my package "Foo" I call a function from another package "Bar" if it's available, according to R-exts, this sounds like I should include "Suggests: Bar" in package Foo's description. But the manual is silent on how to treat Bar's namespace. Should I import it? If so, should this be conditional or unconditional? There is a thread from 2008 in which Duncan Murdoch suggests trying conditionally importing a package if it's installed, with the caveat "If this is allowed" (http://tolstoy.newcastle.edu.au/R/e5/devel/08/10/0488.html). This appears to work in current release of R, 2.15.2, but I'm still not clear if it's officially allowed, much less recommended.
>> 
>> The manual also says:
>> 
>>> If a package only needs a few objects from another package it can use a fully qualified variable reference in the code instead of a formal import. A fully qualified reference to the function f in package foo is of the form foo::f. This is slightly less efficient than a formal import and also loses the advantage of recording all dependencies in the NAMESPACE file, so this approach is usually not recommended. Evaluating foo::f will cause package foo to be loaded, but not attached, if it was not loaded already?this can be an advantage in delaying the loading of a rarely used package.
>>> 
>> 
>> 
>> Would this be a better solution than importing when calling into a suggested package?
>> 
>> Davor
>> 
>> 
>>        [[alternative HTML version deleted]]
>> 
>> 
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>> 
> 
> 
> 
> -- 
> Chief Scientist, RStudio
> http://had.co.nz/
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



[Rd] Recommended way to call/import functions from a Suggested package
Simon Urbanek