Programs->R->Namespaces
Namespaces
Home
  What's New
  Downloads
Programs
  CSS Editor
  TCP/IP
  rInstaller
  R
    Tutorial
    Reference
    Graphics Reference
    Namespaces
  R2
  Java
Publications
  ODBC Browser
  Internet Server

Red Corona
Mail me




Namespaces in R

Introduction to Namespaces

Namespaces are a way to organise a workspace, so that, for instance, utility functions and variables do not hide your own when typing #fns or #vars. They are effectively a group of functions and variables that can “see” each other without using qualified names. For instance, you could define a variable v by typing 'v' 3 #set, change the current namespace to fluffy by typing 'fluffy' #cs, and you would not see v when you typed #vars. This grouping can be useful, for instance to avoid name clashes between utility and user code, or to separate similar pieces of code without resorting to strange naming conventions.

How R Implements Namespaces

A namespace in R can be moved into by using the system function #cs. This allows you to look at all the functions and variables in that namespace, and you can move into a namespace that does not yet exist. To define a namespace, you must define a function or variable in it. For instance, the following code creates a namespace called a, which contains one variable, v:
'a.v' 'Hi there!' #set
The dot (.) operator in the name delimits the parent (namespace) from the child (variable).

To see the namespaces that are currently defined, use the system function #spaces. To find out which namespace you are currently in, use #curns. This leaves the namespace name on the stack so applications can use it.

A namespace tree can be created by a statement such as:
'one.two.v' 42 #set
This creates a variable v in a namespace two that is itself a child of namespace one. To access this variable from the root namespace, you call it with one.two.v, its qualified name. However, if you had typed:
'one.two' #cs
... you could have viewed it by typing:
v.

Functions are run in their own namespace (unless they are based in the default namespace: see later), so you could define a function foo in namespace one.two (which now contains a variable v) as follows:
'one.two.foo' {v} #def
... and run it (using one.two.foo), and you should see 42, the value of one.two.v, in the output field. You could also have run this function by typing:
'one' #cs
two.foo

... and got the same result.

The exception to this is functions in the default namespace or its children. This is a namespace that is searched in for functions and variables if they are not found in the current one. For instance, the following code shows a function being run even though it is not in root:
'default.fn' {1 2 3} #def
fn

This would give 1 2 3 in the display.

The other important point about functions in the default namespace is that they are running in the namespace they were called from. This means that this code:
'defv' 15 #set 'default.defv' 3 #set
'default.f' {defv} #def default.f

... returns 15, not 3. However, if the variable defv was not defined in the current namespace, R would search default next and find the correct value.

Namespaces in R: Summary

  • A namespace can be entered using #cs with its qualified name
  • Namespaces can be hierarchical
  • The list of namespaces currently defined is given by #spaces
  • Most functions run in their own namespace
  • The current namespace can be found using #curns
  • The default namespace is searched in for unlocated functions or variables
  • Functions in default, or a sub-namespace of default, run in the namespace they are called from

Page and site © Richard Smith 2001
Please send any comments to richard@redcorona.com - thank you.
 Top   Home