[R] How to catch a R error in R code

jim holtman jholtman at gmail.com
Wed Oct 10 16:02:39 CEST 2007


You need to follow the posting guide and provide commented, minimal,
self-contained, reproducible code.

I can only guess at what your code looks like, but the following
catches the error:

> z <- try(eval(parse(text="1:20 <- x")))
Error in eval(expr, envir, enclos) : object "x" not found
> str(z)
Class 'try-error'  chr "Error in eval(expr, envir, enclos) : object
\"x\" not found\n"
> z <- try(eval(parse(text="x <- 1:20")))
> str(z)
 int [1:20] 1 2 3 4 5 6 7 8 9 10 ...
>


On 10/10/07, HU,ZHENGJUN <hhu at ufl.edu> wrote:
> Hi All,
>
>  I entered a R statement, e.g. 1:20 = x or log("a") on an HTML
> form and passed it to a R-CGI script. Obviously, neither of both
> is a correct R statement or expression. However, my R-CGI script
> could not return and report the error message to the Web site even
> though it received the statement. I tried to use some R built-in
> functions of try, tryCatch, eval, expression, as.expression,
> parse, deparse, etc. None of them worked.
>
>  Note: when a statement is correct, i.e. x = 1:20 or log(2), the
> same script returned and reported the R output to the Web site.
>
>  If any of you has the experience about the matter or topic,
> could you please tell me how to catch a R error in this case?
>
>  Thank you very much for the help in advance.
>  Howard
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list