[R] Error with repeat lines() in function

Uwe Ligges ligges at statistik.uni-dortmund.de
Fri Sep 24 20:15:45 CEST 2004


Sean Davis wrote:

> Here is an example that seems to reproduce the error:


Well, debugging in apply() isn't funny and I'm tooo lazy at quarter past 
eight on a friday evening. Anyway, you can
(a) do it using a loop (which isn't that inefficient compared to the 
apply() approach), or
(b) the clever (vectorized) way that follows:


plotter <- function(annot,rat1,rat2,rf1,...) {
   par(las=2)
   xmax <- max(annot[,2])
   xmin <- min(annot[,1])
   par(mfrow=c(2,1))
   plot(annot[,1],rat1,type="l",xlab="",ylab="log2 Ratio",...)
   points(annot[,1],rat1)

   yoffset <- ifelse(rf1[,4] == "+", 1, -1)
   color <- ifelse(rf1[,4] == "+", "green", "red")
   segments(rf1[,1], -2-yoffset/10, rf1[,4], -2-yoffset/ 10,
     lwd=2, col=color)
   segments(rf1[,2], -2-yoffset/10, rf1[,3], -2-yoffset/ 10,
     lwd=4, col=color)

   abline(h=0,lty=2)
}


Uwe Ligges







> rf1 <- matrix(sort(abs(round(runif(4)*1000000))),nrow=1)
> annot1 <- sort(abs(round(runif(193)*1000000)))
> annot2 <- annot1 + 70
> annot3 <- cbind(annot1,annot2)
> rat2 <- rnorm(193)
> rat1 <- rnorm(193)
> plotter <-
> function(annot,rat1,rat2,rf1,...) {
>     par(las=2)
>     xmax <- max(annot[,2])
>     xmin <- min(annot[,1])
>     par(mfrow=c(2,1))
>     plot(annot[,1],rat1,type="l",xlab="",ylab="log2 Ratio",...)
>     points(annot[,1],rat1)
>     apply(rf1,1,function(z) {
>       if (z[4]=="+") {
>         color <- 'green'
>         yoffset=1
>       } else {
>         color <- 'red'
>         yoffset=-1
>       }
>        lines(list(x=c(z[1],z[4]),y=c(-2-yoffset/10,-2-yoffset/ 
> 10)),lwd=2,col=color)
>        lines(list(x=c(z[2],z[3]),y=c(-2-yoffset/10,-2-yoffset/ 
> 10)),lwd=4,col=color)
>     })
>     abline(h=0,lty=2)
>   }
> plotter(annot3,rat1,rat2,rf1)
> Error in ans[[1]] : subscript out of bounds
> 
> Enter a frame number, or 0 to exit
> 1:plotter(annot3, rat1, rat2, rf1)
> 2:apply(rf1, 1, function(z) {
> Selection: 0
> 
> On Sep 24, 2004, at 12:05 PM, Uwe Ligges wrote:
> 
>> Sean Davis wrote:
>>
>>> I have a function that does some plotting.  I then add lines to the   
>>> plot.  If executed one line at a time, there is not a problem.  If 
>>> I   execute the function, though, I get:
>>> Error in ans[[1]] : subscript out of bounds
>>> This always occurs after the second lines command, and doesn't 
>>> happen   with all of my data points (some do not have errors).  Any 
>>> ideas?
>>
>>
>> Please give an example how to produce the error,
>> i.e. specify a very small toy example (including generated data and  
>> the call to your function).
>> Many people on this list are quite busy these days and don't want to  
>> think about how to call your function and invent an example ...
>>
>> Uwe Ligges
>>
>>
>>
>>> Thanks,
>>> Sean
>>>  function(x,annot,rat1,rat2,rf,...) {
>>>     par(las=2)
>>>     wh <- which(annot[,5]==x)
>>>     xmax <- max(annot[wh,4])
>>>     xmin <- min(annot[wh,3])
>>>     chr <- annot[wh,2][1]
>>>     wh.rf <- rf$chrom==as.character(chr) & rf$txStart>xmin &   
>>> rf$txEnd<xmax
>>>     par(mfrow=c(2,1))
>>>     plot(annot[wh,3],rat1[wh],type="l",xlab="",ylab="log2   
>>> Ratio",main=x,...)
>>>     points(annot[wh,3],rat1[wh])
>>>     apply(rf[wh.rf,],1,function(z) {
>>>       browser()
>>>       if (z[4]=="+") {
>>>         color <- 'green'
>>>         yoffset=1
>>>       } else {
>>>         color <- 'red'
>>>         yoffset=-1
>>>       }
>>>        lines(list(x=c(z[5],z[6]),y=c(-2-yoffset/10,-2-yoffset/  
>>> 10)),lwd=2,col=color)
>>>        lines(list(x=c(z[5],z[6]),y=c(-2-yoffset/10,-2-yoffset/  
>>> 10)),lwd=2,col=color)
>>>     })
>>>     abline(h=0,lty=2)
>>> }
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide!  
>>> http://www.R-project.org/posting-guide.html
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html




More information about the R-help mailing list