[R] Barplots inside loop - several data errors, workaround needed

Steve Lianoglou mailinglist.honeypot at gmail.com
Fri May 11 04:43:00 CEST 2012


Hi,

On Thu, May 10, 2012 at 8:35 PM, Lee <muellerl at gmail.com> wrote:
> Looking at the documentation for try() I am not sure how it would be best
> applied in this situation. My background is not extensively programming.
> Would writing a function first be appropriate?
>
> Also, I'm not sure just a simple error catch would solve my first problem.
> I do, in fact, need it to plot the barplot based on the table which is
> created above. However, R doesn't like the lack of several columns.
>
> Further guidance would be appreciated.

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

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact



More information about the R-help mailing list