Home ยป C Stack Usage is close to the limit and how to solve it

C Stack Usage is close to the limit and how to solve it

  • by
r pirate
Want to do a random act of kindness? Share this post.
Error: C stack usage <numbers> is too close to the limit

If you are running into this error in R, chances are that you are running a recursive function that runs too deep. I found this page to be particularly good in explaining the concept. But here’s an example:

function pop_pringles:
   if tube == empty:
      end
   eat_pringles
   pop_pringles

In this example, we run the pop_pringles function and we will keep eating it until the tube is empty. It’s a function that calls upon itself until a condition has been met.

Running into the C stack error oftentimes means that you’re letting a recursive function run indefinitely. Here’s some examples I ran into myself and which I found on the web.

Example one: merging a huge data table with another data table that didn’t exist.

dt <- merge(dt, another_dt, by = 'ID', all.x = TRUE)

Example two: Creating a super long string. Instead of the col argument, the sep argument should have been used. (Source: Stackoverflow 14719349)

output_table_subset = mutate(big_data_frame,
     combined_table = paste0(first_part, second_part, col = "_"))

Example three: Two functions calling each other indefinitely. (Source: Stackoverflow 14719349)

change_to_factor <- function(x){
  x <- change_to_character(x)
  as.factor(x)
} 

change_to_character <- function(x){
  x <- change_to_factor(x)
  as.character(x)
}

change_to_character("1")

In other words, check if something is running indefinitely. If that is not the case it might be a package error. For example, the Google Sheets package is known to have thrown this error in certain package versions.

Good luck!

Want to do a random act of kindness? Share this post.