Le 18/03/2012 19:30, Uwe Ligges a écrit :
On 18.03.2012 19:27, Patrick Giraudoux wrote:Le 18/03/2012 18:02, Uwe Ligges a écrit :On 18.03.2012 16:08, Patrick Giraudoux wrote:Le 18/03/2012 14:57, Uwe Ligges a écrit :On 18.03.2012 11:21, Patrick Giraudoux wrote:Hi, I am working at adding namespace to my packages, carefully following the doc "Writing R extensions" and some threads on the web. However I cannot find clear explanation about how to best deal with the import or importFrom functions in the name space. To make it short:To declare dependencies in the description file either after Depends: (packages including functions that are called from a package function) or after Suggests: (packages that are called eg from the doucmentationexamples), works well. In this case one does not need to declare import in the namespace file. However, declaring dependencies sometimes for using only one function in a given package looks a bit over the top, and I though I could use importFrom in the namespace to deal with that. So removing thecorresponding declaration in Depends (description file) and declaringeg importFrom(splancs, inout) in the namespace always led to: * checking package dependencies ... ERROR Namespace dependency not required: 'splancs'Same thing with import(sp). I have the same trouble if I do not removethe declarations in Depends.So, I can hardly follow the meaning of the "Writing R extension" doc:Packages implicitly import the base namespace. Variables exported fromother packages with namespaces need to be imported explicitly using the directives |import| and |importFrom|. The |import| directive imports allexported variables from the specified package(s). Thus the directivesimport(foo, bar)specifies that all exported variables in the packages *foo* and *bar*are to be imported. If only some of the exported variables from apackage are needed, then they can be imported using |importFrom|. Thedirective importFrom(foo, f, g)specifies that the exported variables |f| and |g| of the package *foo*are to be imported. Can anybody tell us the conditions for which import and importFrom commands should be used in the NAMESPACE rather than in the Depends and Suggests declarations of the description file, and the conditions of their applicability (without error message... eg with a working example) ?If you import from another namespace (which is the recommended wayrather than just relying on search order), do not forget to declare itas "Imports" in your DESCRIPTION file. Your package depends on the other one: It could not be loaded without having the other one installed now. Uwe LiggesOK. got it (more or less). With this in the description file: Depends: boot (>= 1.3-4), nlme(>= 3.1-64), rgdal (>= 0.7-8), sp (>= 0.9-97), spdep (>= 0.5-43), splancs (>= 2.01-31)importFrom(splancs, inout) is accepted in the NAMESPACE file and I haveno error message with rcmd check. Irr works also with: Depends: boot (>= 1.3-4), nlme(>= 3.1-64), rgdal (>= 0.7-8), sp (>= 0.9-97), spdep (>= 0.5-43) Imports: splancs (>= 2.01-31) Practically, I a am safe (thanks a lot), but I am however still hungry to learn a bit more. I hardly understand what R does when reading the suggests and imports in the DESCRIPTION file compared to what it does reading the NAMESPACE (my mistake was to believe that one was a substitute for the other). From a practical point of view, why is it important to use import or importFrom in the NAMESPACE when everything looks like working well when declarations are done in the DESCRIPTIONfile only (eg in depends or imports)? It is not crystal clear to me even reading carefully "Writing R extension". DESCRIPTION file is used to getthe "list of packages that will be attached (...) before the current package" (indeed, now, I no longer needs to use 'require' within my package functions). In simple words, what may add the NAMESPACE to that ? Apologize to be so poorly aware (= so much stupid) about the role of each of those declaration spaces.Please read some literature about NAMESPACES, e.g. documentation in Writing R Extensions plus Luke Tierney's articles in R News.ok. Will do it with Luke Tierney's articles. I read Writing R extensions already and also S programming (Venables and Ripley), but indeed should come down to more basic text for dudes :-) .S Programming is excellent, but it was written before Namespaces were introduced to R.Uwe LiggesBest, PatrickA NAMESPACE import imports functionality of another namespace into your own namespace and hence avoids problems caused by name clashes. The DESCRIPTION file is required to derive dependency structure of packages, i.e. derive which package has to be *installed* first etc. Uwe Ligges
Great ! Luke Tierney's article in R news is particularly clear (to me) with nice examples on how namespace avoids variable shadowing. I give the link here for any user/developper who may follow the thread on this issue: http://journal.r-project.org/archive.html Volume 3/1 June 2003, Name Space Management for R by Luke Tierney.
Thanks Uwe for the hint. Best, Patrick ______________________________________________ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel