1

When using recursion in R, it would be useful to have recursive environments as well. For example, in the the below example, it would be useful for the below code to print 1 to 9. That is, the x in the environment of each recursion would be one more than the x in the parent environment. Is there an easy way to modify the code such that this is the case?

x = 1
y = function() {
  print(x)
  x = x + 1
  if (x <= 10) y()
}

Edit: a more complicated situation would just involve more variables:

w = 1
x = 2
y = 3
z = 4
y = function() {
  print(x)
  w = w + 1
  x = w + x
  y = x + y
  z = y + z
  if (w <= 10) y()
}

Now instead of four variables, say there's 50 variables. This couldn't be solved very easily through argument passing.

Edit 2:

In edit 1, what I'm hoping for would be something like this:

global: w = 1, x = 2, y = 3, z = 4

recursion 1: w = 2, x = 4, y = 7, z = 11

recursion 2: w = 3, x = 7, y = 14, z = 25

etc. Excuse math errors.

bramtayl
  • 4,004
  • 2
  • 11
  • 18
  • Maybe `y <- function(x = 1) {print(x); if(x < 9) y(x+1) }` ? – bergant May 20 '15 at 05:59
  • That works, but I want the whole parent environment to be available, not just certain arguments passed between recursions. – bramtayl May 20 '15 at 06:02
  • What do you mean by *available*? – bergant May 20 '15 at 06:06
  • I wonder if this question could solve your request: http://stackoverflow.com/questions/7439110/what-is-the-difference-between-parent-frame-and-parent-env-in-r-how-do-they – bdecaf May 20 '15 at 06:26
  • In the current example the function `y` overwrites the variable `y`. Also any change to a variable in the global environment (such as `w = w + 1`) will only produce local copies of the variables but leave the original ones unchanged. In your case this means that the function will never complete. – Backlin May 20 '15 at 06:34
  • Is the underlying reason for why you want to look at the environments to debug the function `y`? – Backlin May 20 '15 at 06:38
  • Backlin, I understand that the function will never complete. I was hoping for a way to get around this. See the second edit above for what I was hoping for. bdecaf, can you explain that thread is applicable? – bramtayl May 20 '15 at 06:41
  • Backlin (2), no, I'm trying to implement a step-wise Runga-Kutta program. I know this can be done with for loops, but ... – bramtayl May 20 '15 at 06:46
  • (2) I think you should change the title of your question then, e.g. to "How to solve an ODE with step-wise Runge-Kutta in R?". It is usually best to ask "how to cut down a tree?" and state that you have tried an axe rather than to ask "how to use an axe?" since there might be a completely different solution, e.g. "a saw", that does the job better. – Backlin May 20 '15 at 07:27

1 Answers1

1

You could use a for loop and build up vectors of the variable states at each iteration. If it runs for long this might become inefficient however.

w <- 1
x <- 2
y <- 3
z <- 4
while(w[1] <= 10){
  w <- c(w[1] + 1, w)
  x <- c(w[1] + x[1], x)
  y <- c(x[1] + y[1], y)
  z <- c(y[1] + z[1], z)
}
cbind(w, x, y, z)

If you really want to use recursive environments (although I'd prefer a looping solution) you can get around the problem by passing all four variables along in a vector.

y <- function(v=c(w=1, x=2, y=3, z=4)) {
  print(v["x"])
  v["w"] <-      1 + v["w"]
  v["x"] <- v["w"] + v["x"]
  v["y"] <- v["x"] + v["y"]
  v["z"] <- v["y"] + v["z"]
  if (v["w"] <= 10){
    y(v)
  } else {
    v
  }
}
y()
Backlin
  • 14,612
  • 2
  • 49
  • 81