[R] Deleting for() loop in function

Duncan Murdoch murdoch at stats.uwo.ca
Wed Oct 10 22:16:49 CEST 2007


On 10/10/2007 2:03 PM, Alberto Monteiro wrote:
> Jim Holtman wrote:
>>
>> One of the things that you should do is to use Rprof
>> to see where time is being spent. 
>>
> Rprof is great! I didn't know such functionality existed.
> 
> But I am a very destructive user; I think I found a way
> to break Rprof: if I do 
> 
> f <- my.slow.function 
> 
> then call
> 
> f(...)
> 
> Rprof will report that f (and not my.slow.function) was
> called <evil grin>.

But it was f that was called.  Nothing got broken.

> I noticed that because I "tested" Rprof with one of my
> programs that do test cases; these test cases run with
> an array of functions, by calling each function from
> the array - precisely what can't be traced by Rprof :-)

One way around this would be to put the names of the functions in a 
vector, and use do.call to call them, e.g.

names <- c("slow1", "slow2")
Rprof()
for (i in 1:2) do.call(names[i], list() )
Rprof(NULL)
summaryRprof()

This should show slow1 and slow2 in the report, because those are the 
names that end up on the call stack.

Duncan Murdoch



More information about the R-help mailing list