Check if a function is called inside another function

后端 未结 1 749
一生所求
一生所求 2021-01-25 04:57

Let say I have the function

mean_wrapper <- function(x) {
  mean(x)
}

How can I check if the mean function is called?

相关标签:
1条回答
  • 2021-01-25 05:42

    You could trace mean:

    trace(mean, tracer = quote(message("mean was called")))
    mean_wrapper(3)
    #Tracing mean(x) on entry 
    #mean was called
    #[1] 3
    untrace(mean)
    #Untracing function "mean" in package "base"
    

    Instead of a message you can use anything (e.g., assignment to a variable in the enclosing environment) as tracer.

    0 讨论(0)
提交回复
热议问题