Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking out of nested loops in R

Very simple example code (only for demonstration, no use at all):

repeat {
  while (1 > 0) {
    for (i in seq(1, 100)) {
      break # usually tied to a condition
    }
    break
  }
  break
}
print("finished")

I want to break out from multiple loops without using break in each loop separately. According to a similar question regarding python, wrapping my loops into a function seems to be a possible solution, i.e. using return() to break out of every loop in the function:

nestedLoop <- function() {
  repeat {
    while (1 > 0) {
      for (i in seq(1, 100)) {
        return()
      }
    }
  }
}

nestedLoop()
print("finished")

Are there other methods available in R? Maybe something like labeling loops and then specifying which loop to break (like in Java) ?

like image 892
Robomatix Avatar asked Jun 01 '16 14:06

Robomatix


People also ask

How do you break out of a nested loop in R?

Next and Break Statements in R A break statement is used inside a loop (repeat,for,while) to stop the iterations and control flow within a loop. In other words, the break statement will stop executing the block of instructions within a loop and exit the loop.

How do you break a loop out of a nested loop?

There are two steps to break from a nested loop, the first part is labeling loop and the second part is using labeled break. You must put your label before the loop and you need a colon after the label as well. When you use that label after the break, control will jump outside of the labeled loop.

How do you break out of a loop in R?

The R Break statement is very useful to exit from any loop such as For, While, and Repeat. While executing these, if R finds the break statement inside them, it will stop executing the code and immediately exit from the loop.

How do I stop too many nested loops?

Originally Answered: How can I avoid nested "for loop" for optimize my code? Sort the array first. Then run once over it and count consecutive elements. For each count larger than 1, compute count-choose-2 and sum them up.


1 Answers

I think your method of wrapping your nested loops into a function is the cleanest and probably best approach. You can actually call return() in the global environment, but it will throw an error and looks ugly, like so:

for (i in 1:10) {
  for (a in 1:10) {
    for(b in 1:10) {

      if (i == 5 & a == 7 & b == 2) { return() }

    }
  }
}

print(i)
print(a)
print(b)

Which looks like this in the command line:

> for (i in 1:10) {
+   for (a in 1:10) {
+     for(b in 1:10) {
+       
+       if (i == 5 & a == 7 & b == 2) { return() }
+       
+     }
+   }
+ }
Error: no function to return from, jumping to top level
> 
> print(i)
[1] 5
> print(a)
[1] 7
> print(b)
[1] 2

Obviously far better and cleaner to use the function method.

EDIT:

Added an alternative solution to making the error look nicer given by Roland:

for (i in 1:10) {
  for (a in 1:10) {
    for(b in 1:10) {

      if (i == 5 & a == 7 & b == 2) { stop("Let's break out!") }

    }
  }
}

print(i)
print(a)
print(b)
like image 140
giraffehere Avatar answered Oct 21 '22 23:10

giraffehere