Try catch in R

+1 vote
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
• 18,520 points
662 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
• 52,350 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,090 points
4,342 views
+2 votes
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 18, 2017 in Data Analytics by Sudhir
• 1,570 points
872 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,660 points
1,181 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,660 points
5,890 views
+1 vote
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
• 33,030 points
1,813 views
+10 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 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,627 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 Abhi
• 3,720 points
4,537 views
0 votes
1 answer

How to know the type of an object?

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

answered Sep 20, 2018 in Data Analytics by Abhi
• 3,720 points
712 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
• 52,350 points
694 views
0 votes
2 answers

Installing MXNet for R in Windows System

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

answered Dec 4, 2018 in Data Analytics by Kalgi
• 52,350 points
2,426 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP