Skip to content

Barplots inside loop - several data errors, workaround needed

4 messages · jim holtman, Lee, Steve Lianoglou

#
?try

Sent from my iPad
On May 8, 2012, at 22:03, Lee <muellerl at gmail.com> wrote:

            
1 day later
#
Hi,
On Thu, May 10, 2012 at 8:35 PM, Lee <muellerl at gmail.com> wrote:
Consider this block of code that you can run in your R workspace:

~~~~~
set.seed(123)
random.error <- runif(3, 0, 2) > 1.5
for (throws.error in random.error) {
  cat("I'm about to try something\n")
  result <- try({
    cat("  I'm in the middle of trying something\n")
    cat("  There is a chance it might result in an error\n")
    if (throws.error) {
      stop("Error!")
    }
  }, silent=TRUE)
  if (is(result, 'try-error')) {
    cat("An error occurred while trying something, but I'm OK\n\n")
  } else {
    cat("No error occurred while trying something\n\n")
  }
}

~~~~~

and the output it gives:

~~~~~
I'm about to try something
  I'm in the middle of trying something
  There is a chance it might result in an error
No error occurred while trying something

I'm about to try something
  I'm in the middle of trying something
  There is a chance it might result in an error
An error occurred while trying something, but I'm OK

I'm about to try something
  I'm in the middle of trying something
  There is a chance it might result in an error
No error occurred while trying something
~~~~~

Does that help?

-steve