[Rd] How to understand packages, namespaces, environments
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri May 6 19:34:03 CEST 2005
On Fri, 6 May 2005, Alexander Stoddard wrote:
> I would be very glad of pointers to information on how the concepts of
> packages, namespaces and environments are interrelated in R.
> I am trying to get a handle on this both so I can delve further into
> understanding other people's code and so I can organize my own in a more
> coherent manner.
> From my reading about environments it seems they function as what I would
> intuitively call namespaces. However, the documentation for the 'library'
> function implies that "namespace" has a specific meaning in R that I have so
> far failed to grasp. What is that meaning (or where should I look to read up
> on it) ?
Look in `Writing R Extensions' and on developer.r-project.org. I don't
know about `intuitively' but they are similar to other systems (e.g.
More user-level documentation of namespaces (e.g. in R-lang) is on the
WOULD-BE-NICE list, but there is enough in `Writing R Extensions' for many
package writers and we are talking about more details of what happens
under the skin.
> I can think of the following more specific questions. Perhaps they may most
> usefully serve to reveal my misconceptions, so corrections would be very
It seems you have revealed that you have not studied the manuals nor the
help pages. Please do so before posting (see the posting guide).
> Does saying the following load package 'foo' into its own environment ?
> Do environments have names?
Not in general. You will see <environment: some-hex-address> printed
quite often. I am not at all sure you have grasped what an environment is
in R: see the R-lang manual. (Look it up in the index, as at present the
term is used before it is defined.)
> Of what does the list returned by search() actually consist? Is it a list of
> environments, a list that may include environments, or something else?
search() does not return a list: it returns a character vector. It seems
you have not read its help page, which tells you that quite explicitly.
> What is actually designated by a character vector of form "package:foo"?
That is actually a character string, not a character vector. It is a name
that can be used to refer to attach'ed objects, as the search() help page
> In what ways can I use the character vector "package:foo" when interacting
> with R?
I am aware of attach(), detach() and as.environment() (which is used by
quite a few functions, e.g. get()). (Asking what you can do it an
extensible system such as R is not very helpful.) Generally it is a name
used when a package or another attach'ed object might be meant: it is not
used if a package is all that can be specified.
> Many thanks,
> Alex Stoddard
> P.S. It took me a long time of flailing about to discover the 'search'
> function. Perhaps it could be included in the "See Also" section of the help
> for 'ls'.
This *is* in `An Introduction to R', something we ask all R users to read.
I (and I am biased) suggest the right way to get to grips with things like
this is to read a good book on the S/R language.
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-devel