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 <ronggui.hu...@gmail.com> wrote: > 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 Stavros Macrakis <macra...@alum.mit.edu>: >> 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 most elementary and standard case. Clearly I'm >> missing something. >> >> -s >> >> On Thu, Apr 16, 2009 at 9:26 PM, ronggui <ronggui.hu...@gmail.com> wrote: >>> 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 >>> fact(4) >>> X is 4 >>> [1] 24 >>> >>> >>> 2009/4/13 Stavros Macrakis <macra...@alum.mit.edu>: >>>> I would like to trace functions, displaying their arguments and return >>>> value, but I haven't been able to figure out how to do this with the >>>> 'trace' function. >>>> >>>> After some thrashing, I got as far as this: >>>> >>>> fact <- function(x) if(x<1) 1 else x*fact(x-1) >>>> tracefnc <- function() dput(as.list(parent.frame()), # >>>> parent.frame() holds arg list >>>> control=NULL) >>>> trace("fact",tracer=tracefnc,print=FALSE) >>>> >>>> but I couldn't figure out how to access the return value of the >>>> function in the 'exit' parameter. The above also doesn't work for >>>> "..." arguments. (More subtly, it forces the evaluation of promises >>>> even if they are otherwise unused -- but that is, I suppose, a weird >>>> and obscure case.) >>>> >>>> Surely someone has solved this already? >>>> >>>> What I'm looking for is something very simple, along the lines of >>>> old-fashioned Lisp trace: >>>> >>>>> defun fact (i) (if (< i 1) 1 (* i (fact (+ i -1))))) >>>> FACT >>>>> (trace fact) >>>> (FACT) >>>>> (fact 3) >>>> 1> (FACT 3) >>>> 2> (FACT 2) >>>> 3> (FACT 1) >>>> 4> (FACT 0) >>>> <4 (FACT 1) >>>> <3 (FACT 1) >>>> <2 (FACT 2) >>>> <1 (FACT 6) >>>> 6 >>>> >>>> Can someone help? Thanks, >>>> >>>> -s >>>> >>>> ______________________________________________ >>>> R-help@r-project.org mailing list >>>> https://stat.ethz.ch/mailman/listinfo/r-help >>>> PLEASE do read the posting guide >>>> http://www.R-project.org/posting-guide.html >>>> and provide commented, minimal, self-contained, reproducible code. >>>> >>> >>> >>> >>> -- >>> HUANG Ronggui, Wincent >>> PhD Candidate >>> Dept of Public and Social Administration >>> City University of Hong Kong >>> Home page: http://asrr.r-forge.r-project.org/rghuang.html >>> >> > > > > -- > HUANG Ronggui, Wincent > PhD Candidate > Dept of Public and Social Administration > City University of Hong Kong > Home page: http://asrr.r-forge.r-project.org/rghuang.html > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.