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 :

  1. time : time @ function called (sys.time)
  2. loc : function call error recorded (name of function)
  3. 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

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -