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
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
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
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
4 matches
Mail list logo