[R] Count function calls

William Dunlap wdunlap at tibco.com
Wed Feb 27 01:40:59 CET 2013


This is where <<- is helpful:
   > N <- 0 ; trace(solve, quote(N <<- N + 1), print=FALSE)
   Tracing function "solve" in package "base"
   [1] "solve"
   > lapply(3:0, function(i)solve(diag(i,3), 1:3))
   Error in solve.default(diag(i, 3), 1:3) : 
     Lapack routine dgesv: system is exactly singular: U[1,1] = 0
   > N
   [1] 4

You can also set
   options(error=recover)
to look at the state of things when the error occurs.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Simon Zehnder
> Sent: Tuesday, February 26, 2013 2:53 AM
> To: r-help at r-project.org help
> Subject: [R] Count function calls
> 
> Dear R-users,
> 
> I have the following problem: I am running the function 'pvcm' from the 'plm' Panel Data
> package. Inside this function 'solve' is called and gives for a certain individual data series
> an exception because of singularity. I would like to know which individual data series
> causes this error. I tried to debug it, but this is truly painful, as solve is called inside of
> 'lapply' and there are over 5,000 individual data series in the panel.
> 
> Now, what I would like to do is to count the calls to 'solve' inside the function, so I can
> see, where the function throws the exception. I tried to use 'trace' with a count variable,
> but I have no clue how to define a global variable to be used by trace and updated at
> every call.....is there another approach?
> 
> 
> Best
> 
> Simon
> 
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list