Try catch in R

0 votes
Is there a way for error handling in R? Something like a try-catch block like the way in java.
Nov 6, 2018 in Data Analytics by Hannah
• 17,890 points
69 views

1 answer to this question.

0 votes

The most straightforward way is to wrap our problematic call in a tryblock:

> for(input in inputs) {
+   try(print(paste("log of", input, "=", log(input))))
+ }

[[1] "log of 1 = 0"
[1] "log of 2 = 0.693147180559945"
[1] "log of 4 = 1.38629436111989"
[1] "log of -5 = NaN"
Error in log(input) : Non-numeric argument to mathematical function
In addition: Warning message:
In log(input) : NaNs produced
[1] "log of 0 = -Inf"
[1] "log of 10 = 2.30258509299405"

This skips over the error-causing non-numeric input with an error message (you can suppress the error message with the silent=Targument to try), and continues on with the rest of the input. Generally, this is what you would like.

The tryCatch block

Sometimes, however, you might want substitute your own return value when errors (or warnings) are returned. We can do this with tryCatch, which allows you to write your own error and warning handlers. Let’s set our loop to return log(-x) when x is negative (negative arguments throw a warning) and return a NaN for non-numeric arguments (which throw an error). We’ll print out an advisory message, too.

> for(input in inputs) {

+     tryCatch(print(paste("log of", input, "=", log(input))),
+              warning = function(w) {print(paste("negative argument", input)); 
                                      log(-input)},
+              error = function(e) {print(paste("non-numeric argument", input));
                                    NaN})
+ }

[1] "log of 1 = 0"
[1] "log of 2 = 0.693147180559945"
[1] "log of 4 = 1.38629436111989"
[1] "negative argument -5"
[1] "non-numeric argument oops"
[1] "log of 0 = -Inf"
[1] "log of 10 = 2.30258509299405"
answered Nov 6, 2018 by Kalgi
• 51,300 points

Related Questions In Data Analytics

0 votes
1 answer

Catch integer(0) in R

R's way of printing a zero length ...READ MORE

answered Apr 27, 2018 in Data Analytics by kappa3010
• 2,080 points
753 views
+1 vote
1 answer

Need a hadoop engine in backend to run r server

Dear Koushik, Hope you are doing great. The hadoop ...READ MORE

answered Dec 17, 2017 in Data Analytics by Sudhir
• 1,610 points
86 views
0 votes
2 answers

Transforming a key/value string into distinct rows in R

We would start off by loading the ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,560 points
102 views
0 votes
1 answer

Finding frequency of observations in R

You can use the "dplyr" package to ...READ MORE

answered Mar 26, 2018 in Data Analytics by Bharani
• 4,560 points
373 views
0 votes
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 32,440 points
190 views
+9 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 8, 2018 in Data Analytics by Anmol
• 3,680 points
97 views
0 votes
1 answer

How can I perform word stemming in R

The tm package in R provides the stemDocument() function to stem the ...READ MORE

answered Aug 20, 2018 in Data Analytics by Anmol
• 3,680 points
110 views
0 votes
1 answer

How to know the type of an object?

To find the type of an object ...READ MORE

answered Sep 19, 2018 in Data Analytics by Anmol
• 3,680 points
46 views
0 votes
1 answer

How do I check and catch errors in R

With tryCatch you can handle errors as you want: an.error.occured ...READ MORE

answered Nov 6, 2018 in Data Analytics by Kalgi
• 51,300 points
34 views
0 votes
2 answers

Installing MXNet for R in Windows System

You can install it for python in ...READ MORE

answered Dec 3, 2018 in Data Analytics by Kalgi
• 51,300 points
485 views