Re: [R] Using trace

2009-04-17 Thread Stavros Macrakis
Yes, that is similar to the solution in my original posting, but doesn't solve the problem I was having with that solution, namely reporting on the return value. -s On Fri, Apr 17, 2009 at 11:28 AM, ronggui wrote: > Here is a partial solution: > >> trace(fact,quote({cat(sprintf("x= %i

Re: [R] Using trace

2009-04-17 Thread ronggui
Here is a partial solution: > trace(fact,quote({cat(sprintf("x= %i\n",x));return}),print=T) [1] "fact" > fact(4) Tracing fact(4) on entry x= 4 Tracing fact(x - 1) on entry x= 3 Tracing fact(x - 1) on entry x= 2 Tracing fact(x - 1) on entry x= 1 Tracing fact(x - 1) on entry x= 0 [1] 24 2009/4/17

Re: [R] Using trace

2009-04-17 Thread Stavros Macrakis
Well, yes, of course I could add the code to the function by hand. I could also calculate square roots by hand. But -- as in every other basic programming environment -- there exists an R function 'trace' which appears to automate the process, and I can't figure out how to use it to handle this m

Re: [R] Using trace

2009-04-16 Thread ronggui
Can you just print what you need to know? For example: > fact <- function(x) { + if(x<1) ans <- 1 else ans <- x*fact(x-1) + print(sys.call()) + cat(sprintf("X is %i\n",x)) + print(ans) + } > fact(4) fact(x - 1) X is 0 [1] 1 fact(x - 1) X is 1 [1] 1 fact(x - 1) X is 2 [1] 2 fact(x - 1) X is 3 [1] 6