Get Started →
Other Errors

Stack overflow / recursion depth exceeded

Matches: stack overflow|evaluation depth

WHAT THIS ERROR MEANS

Your code has infinite recursion — a function keeps calling itself without a proper stopping condition. R has a limited call stack and this error occurs when it's exceeded.

HOW TO FIX IT

1. Check recursive functions for a proper base case.

2. Look for circular references between functions.

3. Increase the limit with options(expressions = 5000) if recursion is intentional but deep.

CODE EXAMPLES

BAD — THIS CAUSES THE ERROR
f <- function(x) f(x + 1)  # no base case
f(1)
GOOD — CORRECT APPROACH
f <- function(x) {
  if (x > 100) return(x)  # base case
  f(x + 1)
}
f(1)

Still stuck?

Paste your code in RChat and the AI will fix this error in context.

Try RChat Free →

RELATED OTHER ERRORS