67974

tryCatch block in R, returning variable

Question:

So, I am trying to understand scope and functionality of tryCatch in R.

the following line:

arima(rep(1,3), order = c(1,0,0))

generates both warning and error, however in tryCatch block only warning function returns value. How can I get access to return value of both warning and error?

tryTest = tryCatch( { arima(rep(1,3), order = c(1,0,0)) }, warning = function(w) { print('this is warning') print(w) return('return string from warning') }, error = function(e) { print('this is error') print(e) return('return string from error') }, finally = {} ) print(tryTest)

produces only:

"return string from warning"

Answer1:

tryCatch in R allows you to assign a value to the variable on error. Here are two minimal examples:

my_logo <- tryCatch( { my_logo <- RCurl::getURLContent("https://invalid.website") }, error = function(cond){ my_logo <- "there is no image" }, finally = { #pass }) > my_logo [1] "there is no image" my_var <- tryCatch( { my_var <- "a"/1 }, error = function(cond){ my_var <- "foo" }, finally = { #pass }) > my_var [1] "foo"

Similarly, you can return a value on warning as you already know. You should not write your tryCatch statement such that it could encounter both error and warning at the same time. I am not even sure if that is possible.

<hr />

Edit: For completeness, I am adding an example with warning:

my_var <- tryCatch( { warning() my_var <- "a"/1 }, warning = function(cond){ print("There was a warning") return("bar") }, error = function(cond){ my_var <- "foo" print("This message will not be printed.") }, finally = { #pass }) [1] "There was a warning" > my_var [1] "bar"

Recommend

  • AOT and JIT with build-optimizer
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • Mocha throws unexpected token error for ES6 object spread operator
  • Calling Rcpp function with Xptr and Function - only xptr case works
  • Criterion causing memory consumption to explode, no CAFs in sight
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Assign variable to the value in HTML
  • Regex thinks I'm nesting, but I'm not
  • What is Eclipse's Declaration View used for?
  • Counter field in MS Access, how to generate?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Fill an image in a square container while keeping aspect ratio
  • sending/ receiving email in Java
  • htaccess rewriting URLs with multiple forward slashes
  • How to set my toolbar fixed while scrolling android
  • Redux, normalised entities and lodash merge
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • Rearranging Cells in UITableView Bug & Saving Changes
  • align graphs with different xlab
  • Apache 2.4 - remove | delete | uninstall
  • Return words with double consecutive letters
  • AT Commands to Send SMS not working in Windows 8.1
  • How to format a variable of double type
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • apache spark aggregate function using min value
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Reading document lines to the user (python)
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?