[R] skipping an error message

William Dunlap wdunlap at tibco.com
Mon Apr 7 17:35:53 CEST 2014


> Thanks for the suggestion. What I understand is trying something like
> 
> tryCatch(nlme(conc~f2(dose,Theta1,Theta
> 2,Theta3,t),
>  fixed=Theta1+Theta2+Theta3~1,
>  data=grouped.data,
>  random=Theta1+Theta2+Theta3~1,
>  start=list(fixed=ini.pkpara))
>             )
> 
> Is that correct?

No.

Executing tryCatch(someExpression) is equivalent to executing just
someExpression because you haven't set up any condition handlers.

If you did something like
   model.d <- tryCatch(nlme(blah blah blah), error=function(e)NULL)
then model.d would contain the return value of the call to nlme if
all went well and NULL otherwise.  You could then use
   if(!is.null(model.d)) {
       furtherProcessTheModel(model.d)
   }
to do things that only make sense when the model could  be fitted.

Now NULL may be a legitimate return value from a function and
it also doesn't give you any hint about what went wrong, so you could
have the error handler return an object of a class that no normal
function would return and have it contain the error message.  E.g.,
   model.d <- tryCatch(nlme(notAFormula),
                             error=function(e)structure(conditionMessage(e),class="ERROR"))
and then you can test for errors with
   if (inherits(model.d, "ERROR")) {
       message("Iteration ", i, "failed with error: ", model.d)
   } else {
       furtherProcessTheModel(model.d)
   }
You could also look at the error message that model.d contains in that case.

The try() function is essentially that call to tryCatch: it gives its error output
the class 'try-error'.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Naser Jamil
> Sent: Monday, April 07, 2014 1:46 AM
> To: Jim Lemon
> Cc: R help
> Subject: Re: [R] skipping an error message
> 
> Hi Jim,
> Thanks for the suggestion. What I understand is trying something like
> 
> tryCatch(nlme(conc~f2(dose,Theta1,Theta
> 2,Theta3,t),
>  fixed=Theta1+Theta2+Theta3~1,
>  data=grouped.data,
>  random=Theta1+Theta2+Theta3~1,
>  start=list(fixed=ini.pkpara))
>             )
> 
> Is that correct?
> 
> Once again thanks.
> 
> Regards,
> Jamil.
> 
> 
> On 6 April 2014 23:51, Jim Lemon <jim at bitwrit.com.au> wrote:
> 
> > On 04/06/2014 08:21 PM, Naser Jamil wrote:
> >
> >> Dear R-user,
> >> May I seek your suggestion on an issue. I'm fitting non-linear mixed
> >> effects
> >> model as a part of my large R program. But sometimes I get error messages
> >> from it and the code stops working. In such cases, I want to skip the
> >> iterations and
> >> want to move to the next iteration ignoring all the subsequent
> >> computations.
> >>
> >> The following is only that part of the code which fits the mixed effects
> >> model. I tried with "tryCatch" as shown below, but it's not serving my
> >> purpose. I guess something is wrong in my approach.
> >>
> >> ###########################################################
> >>
> >> grouped.data<-groupedData(formula = conc ~ t | subject,
> >> data = data.d)
> >> model.d<-nlme(conc~f2(dose,Theta1,Theta2,Theta3,t),
> >> fixed=Theta1+Theta2+Theta3~1,
> >> data=grouped.data,
> >> random=Theta1+Theta2+Theta3~1,
> >> start=list(fixed=ini.pkpara))
> >> summ<-summary(model.d) # summary of the model
> >>
> >> tryCatch(summ, error = function() next)
> >>
> >> ###########################################################
> >>
> >>  Hi Jamil,
> > I think you have to pass the expression:
> >
> > nlme(conc~f2(dose,Theta1,Theta2,Theta3,t),
> >  fixed=Theta1+Theta2+Theta3~1,
> >  data=grouped.data,
> >  random=Theta1+Theta2+Theta3~1,
> >  start=list(fixed=ini.pkpara))
> >
> > not the result of the expression.
> >
> > Jim
> >
> 
> 	[[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.




More information about the R-help mailing list