Re: [Rd] conditionally import a namespace?
2008/10/31 Duncan Murdoch [EMAIL PROTECTED]: On 10/30/2008 10:44 AM, Duncan Murdoch wrote: On 10/30/2008 10:15 AM, Martin Maechler wrote: FA == Felix Andrews [EMAIL PROTECTED] 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) } ?? I think the problem is that that puts the namespace on the user's search list, which is what Felix wanted to avoid. He would like to import(RGtk2), but only if it exists. There are conditionals allowed in NAMESPACE files, but I don't know if they are allowed to be used to control imports. They are not well documented -- they were called experimental when introduced in 1.9.0. If this is allowed, then something like if( RGtk2 %in% rownames(installed.packages()) ) { import(RGtk2) } should work. I just did a little test, and it does appear to work if you use utils::installed.packages in the condition. And one clarification: those lines go into your NAMESPACE file, not into R code. Many thanks Duncan, that is just what I wanted. The only problem now is that it fails R CMD check: * checking package dependencies ... ERROR Namespace dependencies not required: RGtk2 Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- Felix Andrews / 安福立 http://www.neurofractal.org/felix/ 3358 543D AAC6 22C2 D336 80D9 360B 72DD 3E4C F5D8 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
[Rd] conditionally import a namespace?
Dear R-devel, I have a problem defining the dependencies for a package. My package (latticist, not yet released) Suggests RGtk2, but specifically does not require it. If RGtk2 is available, the user can call a function that builds a GUI with RGtk2. However, I do not want to attach the RGtk2 namespace, because it is irrelevant to the user and exports about 7500 symbols. Last time I asked a similar question to this, Professor Ripley noted that the usual method to get around this situation is the use an explicit package prefix to function calls (the `::` operator). But this is not so easy with non-standard functions. Take this chunk of code: widg - gtkComboBoxEntryNewText() widg$show() widg[width-request] - 100 The first call is easy to prefix, as RGtk2::gtkComboBoxEntryNewText() but the others, `$.RGtkObject` and `[-.RGtkObject` are not. While I *could* rewrite all the code to use explicit functions, I think, the resulting code would be much less clear. Essentially what I want to do is conditionally import the RGtk2 namespace. Any suggestions? Thanks Felix -- Felix Andrews / 安福立 http://www.neurofractal.org/felix/ 3358 543D AAC6 22C2 D336 80D9 360B 72DD 3E4C F5D8 __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] conditionally import a namespace?
FA == Felix Andrews [EMAIL PROTECTED] 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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] conditionally import a namespace?
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 [EMAIL PROTECTED] wrote: FA == Felix Andrews [EMAIL PROTECTED] 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@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel -- Jeffrey Ryan [EMAIL PROTECTED] ia: insight algorithmics www.insightalgo.com __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] conditionally import a namespace?
On 10/30/2008 10:15 AM, Martin Maechler wrote: FA == Felix Andrews [EMAIL PROTECTED] 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) } ?? I think the problem is that that puts the namespace on the user's search list, which is what Felix wanted to avoid. He would like to import(RGtk2), but only if it exists. There are conditionals allowed in NAMESPACE files, but I don't know if they are allowed to be used to control imports. They are not well documented -- they were called experimental when introduced in 1.9.0. If this is allowed, then something like if( RGtk2 %in% rownames(installed.packages()) ) { import(RGtk2) } should work. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] conditionally import a namespace?
On 10/30/2008 10:44 AM, Duncan Murdoch wrote: On 10/30/2008 10:15 AM, Martin Maechler wrote: FA == Felix Andrews [EMAIL PROTECTED] 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) } ?? I think the problem is that that puts the namespace on the user's search list, which is what Felix wanted to avoid. He would like to import(RGtk2), but only if it exists. There are conditionals allowed in NAMESPACE files, but I don't know if they are allowed to be used to control imports. They are not well documented -- they were called experimental when introduced in 1.9.0. If this is allowed, then something like if( RGtk2 %in% rownames(installed.packages()) ) { import(RGtk2) } should work. I just did a little test, and it does appear to work if you use utils::installed.packages in the condition. And one clarification: those lines go into your NAMESPACE file, not into R code. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] conditionally import a namespace?
I noticed there is also an experimental interface that can be used from R (as opposed to the NAMESPACE file). Can't tell from docs whether it allows conditionals: ?.Import On Thu, Oct 30, 2008 at 10:44 AM, Duncan Murdoch [EMAIL PROTECTED] wrote: On 10/30/2008 10:15 AM, Martin Maechler wrote: FA == Felix Andrews [EMAIL PROTECTED] 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) } ?? I think the problem is that that puts the namespace on the user's search list, which is what Felix wanted to avoid. He would like to import(RGtk2), but only if it exists. There are conditionals allowed in NAMESPACE files, but I don't know if they are allowed to be used to control imports. They are not well documented -- they were called experimental when introduced in 1.9.0. If this is allowed, then something like if( RGtk2 %in% rownames(installed.packages()) ) { import(RGtk2) } should work. Duncan Murdoch __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel