[R] Something wrong with my function Please Help

Paul Hiemstra p.hiemstra at geo.uu.nl
Tue Sep 29 09:49:55 CEST 2009


Chunhao Tu wrote:
> Hi R users,
> I try to build a function to compute odds ratio and relative risk however
> something wrong. I stuck for many hours but I really don't know how to solve
> it. Would someone please give me a hint?
>
>   
>> OR.RR<-function(x){
>>     
> +       x <- as.matrix(any(dim(x)==2))
> +       OR<-(x[1,1]*x[2,2])/(x[1,2]*x[2,1])
> +       RR<-(x[1,1]/(sum(x[1,])))/(x[2,1]/(sum(x[2,])))
> +       return(OR);return(RR)
> +       }
>   
>> tt<-matrix(data=1:4,nrow=2,ncol=2)
>> OR.RR(tt)
>>     
> Error in OR.RR(tt) : subscript out of bounds
>
> Many Thanks
> Tu
>   
In addition to Barry Rowlingson:

You can insert the browser() command at any point in the code to drop 
into that environment. In you case you can use:

x = function(arg1, arg2) {
    browser()
    do_stuff(arg1, arg2)
}

Running this will drop you into the environment of function x, allowing 
you to run the function line by line, and allowing you to inspect the 
content of all objects. An alternative to browser() is to use the 
following command:

options(error=recover)

When a program generates an error, you drop into that environment, 
allowing you to better diagnose the problem. Also look at the 
traceback() command, this show you which chain of commands led to the error.

cheers and good luck,
Paul

-- 
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone:  +3130 274 3113 Mon-Tue
Phone:  +3130 253 5773 Wed-Fri
http://intamap.geo.uu.nl/~paul




More information about the R-help mailing list