Greetings, I'm having some difficulties with evaluating very long expressions (Windows/Linux 2.0.1), as seen below, and would greatly appreciate any help, thoughts or work arounds. Let's say that I wanted to see what I would get if I added 1 to itself 498 times. One way of doing this would be to evaluate the expression 1+1+1+...> eval(parse(text = paste(rep(1, 498), collapse = "+")))[1] 498 However, if we try this with 499+ items we get no answer:> a <- eval(parse(text = paste(rep(1, 499), collapse = "+"))) > aError: Object "a" not found And if this eval is passed to any other function, that function exits without error and without returning and object. So it seems that we've reached some upper limit of evaluation terms. While the parser is able to correctly create the long expression, eval does not successfully evaluate it. My problem is that since the evaluator does not return an object, error, or warning, I'm not able to easily code around this problem. Also, I've thought of no easy way to "count" the terms in the expression to see whether we've breached the upper limit or not. If I were able to see if the eval would work on a particular expression, one thing I had considered was to make an eval.long wrapper that peels terms off the right hand side of an overly-long expression until every sub-expression is legal. Thus, to count to 600 I could just add the first 497 terms with the next 103 terms.>eval(parse(text = paste(rep(1, 497), collapse = "+"))) +eval(parse(text = paste(rep(1, 103), collapse = "+"))) [1] 600 But without an error or way of figuring out if the expression would even be evaluated, I'm not sure how to know when to start or stop the peeling process. It also may become more complicated when parentheses are introduced. Any help would be greatly appreciated. Thanks, Robert platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 0.1 year 2004 month 11 day 15 language R Robert McGehee Geode Capital Management, LLC 53 State Street, 5th Floor | Boston, MA | 02109 Tel: 617/392-8396 Fax:617/476-6389 mailto:robert.mcgehee@geodecapital.com This e-mail, and any attachments hereto, are intended for use by the addressee(s) only and may contain information that is (i) confidential information of Geode Capital Management, LLC and/or its affiliates, and/or (ii) proprietary information of Geode Capital Management, LLC and/or its affiliates. If you are not the intended recipient of this e-mail, or if you have otherwise received this e-mail in error, please immediately notify me by telephone (you may call collect), or by e-mail, and please permanently delete the original, any print outs and any copies of the foregoing. Any dissemination, distribution or copying of this e-mail is strictly prohibited.
McGehee, Robert wrote:>>eval(parse(text = paste(rep(1, 498), collapse = "+"))) > > [1] 498 > > However, if we try this with 499+ items we get no answer: > >>a <- eval(parse(text = paste(rep(1, 499), collapse = "+"))) >>a > > Error: Object "a" not found > > And if this eval is passed to any other function, that function exits > without error and without returning and object.Suggest you downgrade to R 1.8.1, which returns an error message: > eval(parse(text = paste(rep(1, 498), collapse = "+"))) [1] 498 > a=eval(parse(text = paste(rep(1, 499), collapse = "+"))) Error in eval(expr, envir, enclos) : evaluation is nested too deeply: infinite recursion? I dont have an R 1.9.x handy at the moment. Baz
"McGehee, Robert" <Robert.McGehee@geodecapital.com> writes:> Greetings, > I'm having some difficulties with evaluating very long expressions > (Windows/Linux 2.0.1), as seen below, and would greatly appreciate any > help, thoughts or work arounds. Let's say that I wanted to see what I > would get if I added 1 to itself 498 times. One way of doing this would > be to evaluate the expression 1+1+1+... > > > eval(parse(text = paste(rep(1, 498), collapse = "+"))) > [1] 498 > > However, if we try this with 499+ items we get no answer: > > a <- eval(parse(text = paste(rep(1, 499), collapse = "+"))) > > a > Error: Object "a" not found > > And if this eval is passed to any other function, that function exits > without error and without returning and object. > > So it seems that we've reached some upper limit of evaluation terms. > While the parser is able to correctly create the long expression, eval > does not successfully evaluate it.> My problem is that since the evaluator does not return an object, error, > or warning, I'm not able to easily code around this problem. Also, I've > thought of no easy way to "count" the terms in the expression to see > whether we've breached the upper limit or not.It's a bug. 1.9.1 had> eval(parse(text = paste(rep(1, 499), collapse = "+")))Error in eval(expr, envir, enclos) : evaluation nested too deeply: infinite recursion / options(expression=)? which also contains the hint about how to raise the limit. You do see it if you do> a <- try(eval(parse(text = paste(rep(1, 499), collapse = "+")))) > a[1] "evaluation nested too deeply: infinite recursion / options(expression=)?" attr(,"class") [1] "try-error" but that's obviously no excuse for not printing the message. The problem appears still to be present in r-devel (the version at hand was dated Jan.12, though).> If I were able to see if the eval would work on a particular expression, > one thing I had considered was to make an eval.long wrapper that peels > terms off the right hand side of an overly-long expression until every > sub-expression is legal.But do you *really* want to do it this way? Why? BTW, it's not really the length of the expression but its depth. The parse tree for 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 is really (((((((1+2)+3)+4)+5)+6)+7)+8). So you get 7 levels of parentheses. You can easily have less deeply nested parentheses: ((1+2)+(3+4))+((5+6)+(7+8)) With that sort of pattern, adding 500 terms requires a nesting no more than 9 levels deep. Persuading R to nest that way is a bit tricky though. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907
McGehee, Robert wrote:>>a <- eval(parse(text = paste(rep(1, 499), collapse = "+")))Another little investigation shows that if you type: 1+1+......+1 at the > prompt (with over 498 '+1's) both R1.8.1 and R2.0.0 produce an error message, so it would appear to be something specific to the eval() function in R 2.0.x suppressing the error message. You can increase the limit by setting options()$expressions - default is 500 here. help(options) says you can go up to 100000, but that doesn't fix the fundamental problem... Time for the gurus to sort this one out... Baz
McGehee, Robert wrote:> Greetings, > I'm having some difficulties with evaluating very long expressions > (Windows/Linux 2.0.1), as seen below, and would greatly appreciate any > help, thoughts or work arounds. Let's say that I wanted to see what I > would get if I added 1 to itself 498 times. One way of doing this would > be to evaluate the expression 1+1+1+... > > >>eval(parse(text = paste(rep(1, 498), collapse = "+"))) > > [1] 498 > > However, if we try this with 499+ items we get no answer: > >>a <- eval(parse(text = paste(rep(1, 499), collapse = "+"))) >>aSee ?options and set: options(expressions = 1000) eval(parse(text = paste(rep(1, 499), collapse = "+"))) Uwe Ligges> Error: Object "a" not found > > And if this eval is passed to any other function, that function exits > without error and without returning and object. > > So it seems that we've reached some upper limit of evaluation terms. > While the parser is able to correctly create the long expression, eval > does not successfully evaluate it. > > My problem is that since the evaluator does not return an object, error, > or warning, I'm not able to easily code around this problem. Also, I've > thought of no easy way to "count" the terms in the expression to see > whether we've breached the upper limit or not. > > If I were able to see if the eval would work on a particular expression, > one thing I had considered was to make an eval.long wrapper that peels > terms off the right hand side of an overly-long expression until every > sub-expression is legal. > > Thus, to count to 600 I could just add the first 497 terms with the next > 103 terms. > >>eval(parse(text = paste(rep(1, 497), collapse = "+"))) + > > eval(parse(text = paste(rep(1, 103), collapse = "+"))) > [1] 600 > > But without an error or way of figuring out if the expression would even > be evaluated, I'm not sure how to know when to start or stop the peeling > process. It also may become more complicated when parentheses are > introduced. > > Any help would be greatly appreciated. > > Thanks, > Robert > > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 0.1 > year 2004 > month 11 > day 15 > language R > > Robert McGehee > Geode Capital Management, LLC > 53 State Street, 5th Floor | Boston, MA | 02109 > Tel: 617/392-8396 Fax:617/476-6389 > mailto:robert.mcgehee@geodecapital.com > > > > This e-mail, and any attachments hereto, are intended for use by the > addressee(s) only and may contain information that is (i) confidential > information of Geode Capital Management, LLC and/or its affiliates, > and/or (ii) proprietary information of Geode Capital Management, LLC > and/or its affiliates. If you are not the intended recipient of this > e-mail, or if you have otherwise received this e-mail in error, please > immediately notify me by telephone (you may call collect), or by e-mail, > and please permanently delete the original, any print outs and any > copies of the foregoing. Any dissemination, distribution or copying of > this e-mail is strictly prohibited. > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel
Try options(expressions = 1000) eval(parse(text = paste(rep(1, 499), collapse = "+"))) -roger McGehee, Robert wrote:> Greetings, > I'm having some difficulties with evaluating very long expressions > (Windows/Linux 2.0.1), as seen below, and would greatly appreciate any > help, thoughts or work arounds. Let's say that I wanted to see what I > would get if I added 1 to itself 498 times. One way of doing this would > be to evaluate the expression 1+1+1+... > > >>eval(parse(text = paste(rep(1, 498), collapse = "+"))) > > [1] 498 > > However, if we try this with 499+ items we get no answer: > >>a <- eval(parse(text = paste(rep(1, 499), collapse = "+"))) >>a > > Error: Object "a" not found > > And if this eval is passed to any other function, that function exits > without error and without returning and object. > > So it seems that we've reached some upper limit of evaluation terms. > While the parser is able to correctly create the long expression, eval > does not successfully evaluate it. > > My problem is that since the evaluator does not return an object, error, > or warning, I'm not able to easily code around this problem. Also, I've > thought of no easy way to "count" the terms in the expression to see > whether we've breached the upper limit or not. > > If I were able to see if the eval would work on a particular expression, > one thing I had considered was to make an eval.long wrapper that peels > terms off the right hand side of an overly-long expression until every > sub-expression is legal. > > Thus, to count to 600 I could just add the first 497 terms with the next > 103 terms. > >>eval(parse(text = paste(rep(1, 497), collapse = "+"))) + > > eval(parse(text = paste(rep(1, 103), collapse = "+"))) > [1] 600 > > But without an error or way of figuring out if the expression would even > be evaluated, I'm not sure how to know when to start or stop the peeling > process. It also may become more complicated when parentheses are > introduced. > > Any help would be greatly appreciated. > > Thanks, > Robert > > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 0.1 > year 2004 > month 11 > day 15 > language R > > Robert McGehee > Geode Capital Management, LLC > 53 State Street, 5th Floor | Boston, MA | 02109 > Tel: 617/392-8396 Fax:617/476-6389 > mailto:robert.mcgehee@geodecapital.com > > > > This e-mail, and any attachments hereto, are intended for use by the > addressee(s) only and may contain information that is (i) confidential > information of Geode Capital Management, LLC and/or its affiliates, > and/or (ii) proprietary information of Geode Capital Management, LLC > and/or its affiliates. If you are not the intended recipient of this > e-mail, or if you have otherwise received this e-mail in error, please > immediately notify me by telephone (you may call collect), or by e-mail, > and please permanently delete the original, any print outs and any > copies of the foregoing. Any dissemination, distribution or copying of > this e-mail is strictly prohibited. > > ______________________________________________ > R-devel@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Roger D. Peng http://www.biostat.jhsph.edu/~rpeng/
Thank you all for your reply. This was exactly what I was looking for. Two quick points. One, as Peter Dalgaard pointed out, wrapping the expression in a try() gives this error: Error in eval(expr, envir, enclos) : evaluation nested too deeply: infinite recursion / options(expression=)? ^^^ However, from the following posts, it seems that options(expressions=) (plural) is what we're looking for instead. If so, this error message should be corrected anyway. Secondly, the ?options help (thanks for everyone who reminded me about this), says that expressions can have values between 25...100000. However, if the original example is set past 4995 on my computers, I receive a stack overflow.> eval(parse(text = paste(rep(1, 4996), collapse = "+")))Error: protect(): stack overflow I'm pointing this out not because I will ever approach anywhere near this level of nesting, but perhaps that options(expression=5000+) might not be meaningful anyway, (unless there are processor/compilation-specific ways of getting rid of this stack overflow). If so, one might considering lowering the range of valid expressions values in the help file. Thanks again, your replies are, as always, invaluable, Robert -----Original Message----- From: Peter Dalgaard [mailto:p.dalgaard@biostat.ku.dk] Sent: Monday, January 24, 2005 5:36 AM To: McGehee, Robert Cc: r-devel@stat.math.ethz.ch Subject: Re: [Rd] Very Long Expressions "McGehee, Robert" <Robert.McGehee@geodecapital.com> writes:> Greetings, > I'm having some difficulties with evaluating very long expressions > (Windows/Linux 2.0.1), as seen below, and would greatly appreciate any > help, thoughts or work arounds. Let's say that I wanted to see what I > would get if I added 1 to itself 498 times. One way of doing thiswould> be to evaluate the expression 1+1+1+... > > > eval(parse(text = paste(rep(1, 498), collapse = "+"))) > [1] 498 > > However, if we try this with 499+ items we get no answer: > > a <- eval(parse(text = paste(rep(1, 499), collapse = "+"))) > > a > Error: Object "a" not found > > And if this eval is passed to any other function, that function exits > without error and without returning and object. > > So it seems that we've reached some upper limit of evaluation terms. > While the parser is able to correctly create the long expression, eval > does not successfully evaluate it.> My problem is that since the evaluator does not return an object,error,> or warning, I'm not able to easily code around this problem. Also,I've> thought of no easy way to "count" the terms in the expression to see > whether we've breached the upper limit or not.It's a bug. 1.9.1 had> eval(parse(text = paste(rep(1, 499), collapse = "+")))Error in eval(expr, envir, enclos) : evaluation nested too deeply: infinite recursion / options(expression=)? which also contains the hint about how to raise the limit. You do see it if you do> a <- try(eval(parse(text = paste(rep(1, 499), collapse = "+")))) > a[1] "evaluation nested too deeply: infinite recursion / options(expression=)?" attr(,"class") [1] "try-error" but that's obviously no excuse for not printing the message. The problem appears still to be present in r-devel (the version at hand was dated Jan.12, though).> If I were able to see if the eval would work on a particularexpression,> one thing I had considered was to make an eval.long wrapper that peels > terms off the right hand side of an overly-long expression until every > sub-expression is legal.But do you *really* want to do it this way? Why? BTW, it's not really the length of the expression but its depth. The parse tree for 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 is really (((((((1+2)+3)+4)+5)+6)+7)+8). So you get 7 levels of parentheses. You can easily have less deeply nested parentheses: ((1+2)+(3+4))+((5+6)+(7+8)) With that sort of pattern, adding 500 terms requires a nesting no more than 9 levels deep. Persuading R to nest that way is a bit tricky though. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907