R function call

This should be easy, but cannot find direct answers on google or SO.

Imagine that in R I run a function FOuter(), and inside its body it executes some loop and calls another function FInner(). Is there an easy way to count / record the number of calls FInner? I am trying to estimate how much time I can save if I optimize FInner.

+3
source share
1 answer

You are looking for trace.

f1 <- function() 1
f2 <- function() {
    for(i in 1:10) f1()
}

.count <- 0
trace(f1, tracer=function() .count <<- .count +1)
f2()
.count
# 10
untrace(f1)
+4
source

All Articles