[Rd] error location

Thomas Lumley tlumley at u.washington.edu
Fri Sep 29 15:51:02 CEST 2006

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).


More information about the R-devel mailing list