[R] Function completely locks up my computer if the input is too big

Peter Alspach Peter.Alspach at plantandfood.co.nz
Tue Mar 5 22:44:14 CET 2013


Tena koe Benjamin

I haven't looked at you code in detail, but in general ifelse is slow and can generally be avoided.  For example,

ben <- 1:10^7
system.time(BEN <- ifelse(ben<10, NA, -ben))
   user  system elapsed 
   1.31    0.24    1.56 
system.time({BEN1 <- -ben; BEN1[BEN1> -10] <- NA})
   user  system elapsed 
   0.17    0.03    0.20 
all.equal(BEN, BEN1)
[1] TRUE

HTH ...

Peter Alspach

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Benjamin Caldwell
Sent: Wednesday, 6 March 2013 10:18 a.m.
To: r-help
Subject: [R] Function completely locks up my computer if the input is too big

Dear r-help,


Somewhere in my innocuous function to rotate an object in Cartesian space I've created a monster that completely locks up my computer (requires a hard reset every time). I don't know if this is useful description to anyone - the mouse still responds, but not the keyboard and not windows explorer.

The script only does this when the input matrix is large, and so my initial runs didn't catch it as I used a smaller matrix to speed up the test runs.
When I tried an input matrix with a number of dimensions in the same order of magnitude as the data I want to read in, R and my computer choked. This was a surprise for me, as I've always been able to break execution in the past or do other tasks. So i tried it again, and still no dice.

Now I need the function to work as subsequent functions/functionality are dependent, and I can't see anything on the face of it that would likely cause the issue.

Any insight on why this happens in general or specifically in my case are appreciated. Running R 15.2, Platform: x86_64-w64-mingw32/x64 (64-bit) on a windows 7 machine with 4 mb RAM. In the meantime I suppose I'll write a loop to do this function piece-wise for larger data and see if that helps.

Script is attached and appended below.

Thanks

Ben Caldwell



#compass to polar coordinates

compass2polar <- function(x) {-x+90}



#degrees (polar) to radians

Deg2Rad <- function(x) {(x*pi)/180}



# radians to degrees

Rad2Deg <- function (rad) (rad/pi)*180



# polar to cartesian coordinates - assumes degrees those from a compass.
output is a list, x & y of equal length

Pol2Car <- function(distance,deg) {


rad <- Deg2Rad(compass2polar(deg))

rad <- rep(rad, length(distance))


x <- ifelse(is.na(distance), NA, distance * cos(rad))

y <- ifelse(is.na(distance), NA, distance * sin(rad))


x<-round(x,2)

y<-round(y,2)


cartes<- list(x,y)

name<-c('x','y')

names(cartes)<-name

cartes

}





#rotate an object, with assumed origin at 0,0, in any number of degrees

rotate <- function(x,y,tilt){ 8


d2 <- x^2+y^2

rotate.dis<-sqrt(d2)

or.rad <- atan(x/y)

or.deg <- Rad2Deg(or.rad)


n <- length(or.deg)

for(i in 1:n){

if(is.na(or.deg[i])==TRUE) {or.deg[i] <- 0}

}

# browser()

tilt.in <- tilt + or.deg


xy<-Pol2Car (distance=rotate.dis, deg=tilt.in)

 # if(abs(tilt) >= 0) {

 # shift.frame <- cbind(xy$x, xy$y)

# shift.frame.val <- shift.frame[shift.frame[,2]==min(shift.frame[,2]),]

# shift.x<- shift.frame.val[1] * -1

# shift.y<- shift.frame.val[2] * -1

# x<-xy$x + shift.x

# y<-xy$y + shift.y

# }

# name <- c('x', 'y')

# xy<-list(x,y)

# names(xy)<-name

 xy

}


x <- seq(0,5, .5)

y <- seq(0,5, .5)

z <- seq(0,5, .5)

dquad<-expand.grid(x,y,z)

name<-c("y","x","z")

names(dquad)<-name


plot(dquad$x, dquad$y, xlim=c(-25,25), ylim=c(-25,25))


#this works fine

rotated<-rotate(dquad$x, dquad$y, 45)



points(rotated$x, rotated$y, col='green')


# profiling of both time and memory

Rprof("myFunction.out", memory.profiling=T)

y <- myFunction(x)

Rprof(NULL)

summaryRprof("myFunction.out", memory="both")



#############

x <- seq(0,5, .1)

y <- seq(0,5, .1)

z <- seq(0,5, .1)

dquad<-expand.grid(x,y,z)

name<-c("y","x","z")

names(dquad)<-name

# running the below locks up my machine (commented out to avoid accidental
run)

# rotated<-rotate(dquad$x, dquad$y, 45)

The contents of this e-mail are confidential and may be ...{{dropped:14}}



More information about the R-help mailing list