Re: [Rd] conditionally import a namespace?

From: Jeff Ryan <jeff.a.ryan_at_gmail.com>
Date: Thu, 30 Oct 2008 09:21:23 -0500

Or something along the lines of:

Suggests: RGtk2

f <- function (x, ...)
{

    stopifnot("package:RGtk2" %in% search() || require("RGtk2", quietly = TRUE))      # do Rgtk2 stuff here
}

Jeff

On Thu, Oct 30, 2008 at 9:15 AM, Martin Maechler <maechler_at_stat.math.ethz.ch> wrote:
>>>>>> "FA" == Felix Andrews <felix@nfrac.org>
>>>>>> on Thu, 30 Oct 2008 17:40:17 +1100 writes:
>
> FA> Dear R-devel,
> FA> I have a problem defining the dependencies for a package.
>
> FA> My package (latticist, not yet released) "Suggests" RGtk2, but
> FA> specifically does not require it. If RGtk2 is available, the user can
> FA> call a function that builds a GUI with RGtk2. However, I do not want
> FA> to attach the RGtk2 namespace, because it is irrelevant to the user
> FA> and exports about 7500 symbols.
>
> FA> Last time I asked a similar question to this, Professor Ripley noted
> FA> that the usual method to get around this situation is the use an
> FA> explicit package prefix to function calls (the `::` operator). But
> FA> this is not so easy with non-standard functions. Take this chunk of
> FA> code:
>
> FA> widg <- gtkComboBoxEntryNewText()
> FA> widg$show()
> FA> widg["width-request"] <- 100
>
> FA> The first call is easy to prefix, as RGtk2::gtkComboBoxEntryNewText()
> FA> but the others, `$.RGtkObject` and `[<-.RGtkObject` are not.
>
> indeed.
>
> FA> While I *could* rewrite all the code to use explicit functions, I
> FA> think, the resulting code would be much less clear.
>
> FA> Essentially what I want to do is conditionally import the RGtk2 namespace.
> FA> Any suggestions?
>
> Maybe something along the line of
>
> if(is.function(try(RGtk2::gtkComboBoxEntryNewText))) {
> library(RGtk2)
> ....
> ....
> }
>
> ??
>
> Regards,
> Martin
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Jeffrey Ryan
jeffrey.ryan_at_insightalgo.com

ia: insight algorithmics
www.insightalgo.com

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Thu 30 Oct 2008 - 14:23:47 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 30 Oct 2008 - 14:30:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive