From tlumley at u.washington.edu  Fri Sep 29 15:51:02 2006
From: tlumley at u.washington.edu (Thomas Lumley)
Date: Fri, 29 Sep 2006 06:51:02 -0700 (PDT)
Subject: [Rd] error location
In-Reply-To: <ed28ff68ff6.ff6ed28ff68@uni-bielefeld.de>
References: <ed28ff68ff6.ff6ed28ff68@uni-bielefeld.de>
Message-ID: <Pine.LNX.4.64.0609290644040.18499@homer22.u.washington.edu>

On Fri, 29 Sep 2006, Peter Stencel wrote:

> Dear all,
>
> I've written a (quite long) function in R. While running it I get the
> following error message:
>
> Error in "[<-"(`*tmp*`, 1, 13, value =
> NULL) :
>        number of items to replace is not a multiple of
> replacement length
>   operation failed in: function ()  !!!
>   evaluation not successful!!!
>
> How to interpret this error message? (There aren't any expressions that
> I'm using in my function.)
>
> How can I locate the error? Is it possible to do it with the command
> debug("functionname")?
>
> I'm new to R and I hope that someone can give me a piece of advice on
> finding errors. Thanks in advance.
>

The first step after you get an error is

   R>   traceback()


This will give a stack trace, ie, it will show the function where the 
error occurred, the function that called it, the function that called it, 
and so on all the way up.  At some point in the list there should be an 
expression that you recognize.

traceback() is often sufficient.  When it isn't, the post-mortem debugger, 
which you use by setting options(error=recover), may help.  After the 
error occurs this gives you the opportunity to run the debugger inside any 
of the functions that were executing. The debugger is basically an R 
prompt running inside the function, so you can do anything you could do in 
R. debug(f) starts the debugger whenever the function f() is called. This 
lets you get in before the error.

There is a section on debugging in the "Writing R Extensions" manual (in 
sufficiently recent versions of R).

 	-thomas




[Rd] error location
Thomas Lumley