Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R count function calls

Tags:

r

callstack

This should be easy, but can't find any straight-forward answers on google or SO.

Imagine in R I run a function FOuter(), and within its body, it does some loop and calls another function FInner(). Is there a simple way of counting/recording the number of times FInner gets called? I'm trying to estimate how much time I can save if I optimize FInner.

like image 403
bigO6377 Avatar asked Feb 10 '14 20:02

bigO6377


People also ask

What is the count function in R?

Source: R/count-tally.R. count.Rd. count() lets you quickly count the unique values of one or more variables: df %>% count(a, b) is roughly equivalent to df %>% group_by(a, b) %>% summarise(n = n()) . count() is paired with tally() , a lower-level helper that is equivalent to df %>% summarise(n = n()) .

What package is count in R?

count package:plyr R Documentation Count the number of occurences. Description: Equivalent to 'as. data. frame(table(x))', but does not include combinations with zero counts.


1 Answers

You're 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)
like image 200
Matthew Plourde Avatar answered Nov 12 '22 22:11

Matthew Plourde