An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120508/1b1c2214/attachment.pl>
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:
I have a series of data which is managed through a loop. The loop creates "pivot tables" of my data using the *cast* function in the *reshape*library. For the most part, the data is all plotted correctly. Unfortunately, there are a couple of data sets which create errors and halt the loop. One of the tables looks like the following: dbh Black Walnut 1 8 38.19722 2 10 48.89244 3 12 38.19722 When the loop attempts the barplot() function, the following error is returned: Error in seq_len(p) : argument must be coercible to non-negative integer In other cases, there is simply no data in the specified set of data. Therefore, the table is full of NA's. Obviously, this does not need to be plotted, but I cannot simply remove it from the larger database. I need my loop to continue regardless of if it runs into these issues. *Question: What can I do to ensure the above single variable table will plot correctly? and what can I do to suppress errors on the datasets which do not have data so the loop continues?* full code: http://pastebin.com/LB88hpfM Thank you in advance. -- all the best, Lee Mueller ISA Certified Arborist MI-4148A Registered Forester #46043 [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
1 day later
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120510/68c1dfe1/attachment.pl>
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