Thanks to Mollie and a few others who wrote in recommending the try() and 
tryCatch() functions. They work great!

I recognize my question was a basic one, but there are so many useful R 
functions, and the listserv is a great place to find new and useful ones I'd 
never encountered before.

Thanks again,
Phil


On 7/25/2013 10:07 AM, Mollie Brooks wrote:
Hi Phil,
Check out the try() function.
cheers,
Mollie

------------------------
Mollie Brooks, PhD
Postdoctoral Researcher, Population Ecology Research Group 
http://www.popecol.org
Institute of Evolutionary Biology & Environmental Studies, University of Zürich



On 25 Jul 2013, at 3:32 PM, "Novack-Gottshall, Philip M." 
<pnovack-gottsh...@ben.edu<mailto:pnovack-gottsh...@ben.edu>> wrote:

Hi all,

I'm wondering if anyone knows of a function to continue after a
stop/error call in a loop. Something akin to next, but able to deal with
a stop-error.

I'm analyzing many samples, and some statistics/models can not be
applied to certain ones. The reasons why vary across samples, so it's
not as straightforward as adding a rule like "if sample size is < x, go
to next sample." It would be easier if there was a rule like "if the
following function results in an error, go to next sample." However, all
"stops" I know of permanently end the execution, and so adding a "next"
doesn't seem to work.

Here's a contrived example showing what I mean, which executes the first
two cases, but the third "stops", preventing execution for the fourth case:

library(MASS)
set.seed(314)
size <- c(10, 20, 1, 5)
for (i in 1:4) {
    print(fitdistr(rnorm(size[i], mean=20), "gamma"))
    }

# One solution would be to add a line like:
if(size[i] < 1) next

# But in my case, there would have to be many different such "next"
rules, and some I can't figure out exactly why an error is being produced

# Can anyone point me in a useful direction? I'm looking for something like:
if(error) next

Thanks,
Phil

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Phil Novack-Gottshall
Assistant Professor
Department of Biological Sciences
Benedictine University
5700 College Road
Lisle, IL 60532

pnovack-gottsh...@ben.edu<mailto:pnovack-gottsh...@ben.edu>
Phone: 630-829-6514
Fax: 630-829-6547
Office: 332 Birck Hall
Lab: 107 Birck Hall
http://www1.ben.edu/faculty/pnovack-gottshall
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


_______________________________________________
R-sig-ecology mailing list
R-sig-ecology@r-project.org<mailto:R-sig-ecology@r-project.org>
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology




--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Phil Novack-Gottshall
 Assistant Professor
 Department of Biological Sciences
 Benedictine University
 5700 College Road
 Lisle, IL 60532

 pnovack-gottsh...@ben.edu<mailto:pnovack-gottsh...@ben.edu>
 Phone: 630-829-6514
 Fax: 630-829-6547
 Office: 332 Birck Hall
 Lab: 107 Birck Hall
 http://www1.ben.edu/faculty/pnovack-gottshall
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


        [[alternative HTML version deleted]]

_______________________________________________
R-sig-ecology mailing list
R-sig-ecology@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-ecology

Reply via email to