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
Post a Comment