r - Logging and writing error messages to a dataframe -
i intend record errors in r code while calling functions in dataframe (err_log, say). want use 'try' identify errors while calling function,if any.the dataframe(err_log) have following columns :
- time : time @ function called (sys.time)
- loc : function call error recorded (name of function)
- desc : description of error r throws @ (error message in r)
example :
first initialize blank dataframe 'err_log' these columns
then write function
f <- function(a){ x <- a*100 return(x) }
now put output of call 'f' in 'chk'
chk <- try(f())
the above call gives error 'error in * 100 : 'a' missing' (description of error)
check
if(inherits(chk,'try-error')) {then want populate err_log , stop code execution}
how can done in r?
use trycatch
instead of try
then inside trycatch()
, use argument error=function(e){}
e
have element named message,
use following call browser explore e$message
:
x <- trycatch(stop("this error message"), error=function(e) {browser()})
note function need not anonymous.
myerrorparser <- function(e) { m <- e$message if (grepl("something", m)) return (something_else) } ## trycatch(stop("this test"), error=myerrorparser)
Comments
Post a Comment