[R] recursive functions and global variables

David A Richmond richmond at saintmarys.edu
Sun Oct 14 19:44:09 CEST 2001


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. 

dave



#start
b<-0
rm(b)

test <- function(x) {
	b <- x^2
	test2()
}

test2 <- function() {
	return(b+1.1)
}	

print(test(3))
#end

returns:
Error in test2() : Object "b" not found

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|David Richmond                It works on a          |      
+ Dept. of Sociology          complex scientific      + 
|Saint Mary's College          principle, known as    |  
+ Notre Dame, IN 46556               "pot luck."      +
|219-284-4517                    - The Doctor         |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list