Capture Unprotected Error

I want to write the error from D%4 and move on. Mistake:

 Error: unexpected input in "D%4" 

Typically, if a function is called, the following works:

 capture_warn_error <- function(x){ tryCatch({ x }, warning = function(w) { w }, error = function(e) { e }) } capture_warn_error(D%4) 

But recovery is not possible, since `D% 4 immediately disables everything:

 ## > capture_warn_error(D%4) ## Error: unexpected input in "capture_warn_error(D%4)" 

In any case, to capture such a stubborn beast and move on? I know that D%4 not an object, but this works for other non-objects:

 capture_warn_error(means) ## <simpleError in doTryCatch(return(expr), name, parentenv, handler): object 'means' not found> 

Nicely:

  • Understand why D%4 recovering vs means
  • Find a recovery method and capture D%4 error
+7
r
source share
3 answers

As others have argued, this is because text typed on the console is passed to the parser. D%4 fails to rigorously test the correct R expression because a single % invalid inside the R name (although it will create a token that would be interpreted as a user-defined function if there was a % closure) and % also not a function name (although %% is). An error tryCatch while processing the argument of your function and therefore it never reached the internal tryCatch -call. I initially did not understand that you want to parse this input as R-code, so I thought it was easy to wrap readline , since the only argument that needs to be entered can satisfy:

 mfun <- function( x=readline(">>+ ") ){ print(x) } mfun() #-----screen will display-------- >>+ D%4 [1] "D%4" 

If I am mistaken in your intentions, as this appears when I read the question again, then this will lead to the creation of this input mechanism in your capture_error function. This leads to the fact that these characters are integral text, and then performs parse-eval in the tryCatch application:

 > capture_warn_error <- function(x=readline(">>+ ")){ + tryCatch({ eval(parse(text=x)) + + }, warning = function(w) { + w + }, error = function(e) { + e + }) + } > capture_warn_error(D%4) Error: unexpected input in "capture_warn_error(D%4)" > capture_warn_error() >>+ D%4 <simpleError in parse(text = x): <text>:1:2: unexpected input 1: D%4 ^> > err <- capture_warn_error() >>+ D%4 > err <simpleError in parse(text = x): <text>:1:2: unexpected input 1: D%4 ^> > err <- capture_warn_error() >>+ D %% 4 > err <simpleError in D%%4: non-numeric argument to binary operator> > err <- capture_warn_error() >>+ 4 %smthg% 2 > err <simpleError in eval(expr, envir, enclos): could not find function "%smthg%"> 

As shown above, this requires that you do not provide any input in the argument list for calling the function, but rather make a capture call with an empty argument list.

+4
source share

You can configure the input capture function and parse it by wrapping it in the capture_warn_error function.

 getfunction <- function(){ x<-readline() if(x == "exitnow"){return("bye!")} print(capture_warn_error(eval(parse(text = x)))) getfunction() } 

Now they will print on the console invitation, but it will work fine - tasks will require work.

 1+1 [1] 2 d%e <simpleError in parse(text = x): <text>:1:2: unexpected input 1: d%e ^> exitnow [1] "bye!" 
+3
source share

I think this works to capture the error, although this is a bit confusing.

 a <- try(eval(parse(text="D%4"))) 
0
source share

All Articles