Evaluation nested too deeply error for basic formula with many variables

I have the following error showing up in Rstudio when I run the code below:
Error: evaluation nested too deeply: infinite recursion / options(expressions=)?

redun.formula <- formula(paste("~", paste0("X", seq(1:2500), collapse = "+")))

I have tested it on 3 computers (2 OSX and 1 Windows) with recent (different but all 2023) versions of RStudio and R and more than 32GB memory each with the same result. Furthermore, if I try to open a project created a year ago with formulas with many variables, I get the same error when I try to load the .Rdata file, when it used to work perfectly.

On the other hand, the same line of code works with no issue if I run it in a R console.

I have tried to set a larger value than the default one in options(expressions=) as suggested in a few posts talking about this error, and this yields to a different error: Error: C stack usage 7954392 is too close to the limit

Since I have updated RStudio on all computers where the command used to work, I am suspecting there have been a breaking change in one of the updates, but I have no clue what happened nor what I can do to solve the issue. Any suggestion would be welcome.

Strange, I get the same result but why not just

paste("~", paste0("X", seq(1:2500), collapse = "+"))

I need to transform the string into a formula to use it in various functions (Hmisc::redun, gbm::gbm among others). If I leave it as a string, the error occurs when using the function that tries to convert it.

Also note that you can make it work with a smaller number of variables (e.g. 1000 works). The real use case consists of reading colnames from a data frame and in this case, the error occurs after adding more than 600 variables with longer names. Not sure why it makes a difference but it does.

Let's hope someone with more knowledge than me comes along but if not it looks like a Posit github report.

I think I see why the paste option is not working. it is producing a single character variable.

Sorry to be of no help

You were actually helpful: I have tried to pass formula(string) after defining the string as you suggested within the redun() function and it is running without error for now. It might just be a matter of time as the task might take a while to complete. I will report the outcome when the analysis is complete.

I thought of that and said, "No probably won't work" so did not try it. Stupidity on my part!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.