r - Get function being used in error (from call) -


i want extract name of function being used error. if had:

mean(letters) "p" * 5 

i'd want extract "mean.default" , "*". can call error follows:

capturer <- function(x){     trycatch({         x     }, warning = function(w) {         w     }, error = function(e) {         e     }) }  capturer(mean(letters))$call ## mean.default(letters)  capturer("p" * 5)$call ## "p" * 5 

but don't have way grab function names.

you can grab function name part $call[[1]]. add deparse argument add option of having result returned string.

capturer <- function(x, deparse = false) {     out <- trycatch({         x     }, warning = function(w) {         w$call[[1]]     }, error = function(e) {         e$call[[1]]     })     if(deparse) deparse(out) else out }  ## these return call capturer("p" * 5) # `*` capturer(mean(letters)) # mean.default  ## these return character capturer("p" * 5, deparse = true) # [1] "*" capturer(mean(letters), deparse = true) # [1] "mean.default" 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -