Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.A41.4.33.0110141142250.24836-100000@homer18.u.washington.edu>
Date: 2001-10-14T18:49:49Z
From: Thomas Lumley
Subject: recursive functions and global variables
In-Reply-To: <Pine.GSO.4.20.0110141232360.26958-100000@jade.saintmarys.edu>

On Sun, 14 Oct 2001, David A Richmond wrote:

> Hi,
> 	I am trying to write a recursive routine which passes some
> variables through the function calls but also refers to some global
> variables. I need the global variables because they are very big (n by n
> matrices where n is <= 1000) and I don't have the huge amount of memory
> needed to spawn dozens of copies of these matrices. The problem seems to
> be that while variables in the root environment of R are global, variables
> created in a function func1() are not available in functions called by
> func1(). (See the example below.) So I need a way to create some variables
> at the root level of a function that are available at any level
> 'underneath' the base function level. Note also that I want to be able to
> modify elements that are at a higher level as well.
>

You probably want to use a named environment. There's an example in the
`Programmer's Niche' section of the 2nd R newsletter, on the R home page.

Basically, you create an environment like

   storage<-new.env()

and pass it down through your functions. You can then use get() and
assign() to retrieve and modify objects in this environment.

This is often the R way to handle issues for which S-PLUS programmers
would use 'frame 0' and 'frame 1'.


	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._