2
votes

I'm trying to run the following code in R (taken from the bottom of the R Wikipedia page) as I'd like to play around with it myself once I get it up and running:

library("caTools")
jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F",
                             "yellow", "#FF7F00", "red", "#7F0000"))
dx <- 400                  # define width
dy <- 400                  # define height
C <- complex( real=rep(seq(-2.2, 1.0, length.out=dx), each=dy ),
              imag=rep(seq(-1.2, 1.2, length.out=dy), dx ) )
C <- matrix(C,dy,dx)       # reshape as square matrix of complex numbers
Z <- 0                     # initialize Z to zero
X <- array(0, c(dy,dx,20)) # initialize output 3D array
for (k in 1:20) {          # loop with 20 iterations
  Z <- Z^2+C               # the central difference equation
  X[,,k] <- exp(-abs(Z))   # capture results
}
write.gif(X, "Mandelbrot.gif", col=jet.colors, delay=900)

I have succesfully downloaded and installed the caTools package. The R console confirms this with the message

package ‘caTools’ successfully unpacked and MD5 sums checked

and it tells me the location where it is stored.

However, it then complains that:

Error in write.gif(X, "Mandelbrot.gif", col = jet.colors, delay = 900) : 
  object 'X' not found

I cannot see any problem with this - isn't X clearly defined in the above code?

Thanks for any help you can offer.

Your code works for me in the sense that I get a 800 kb .gif file. - Roman Luštrik
@RomanLuštrik Does that mean there is a problem with caTools? I get absolutely nothing :( - user11128
It works for me too. Have you tried the "easy" workaround? (restart R, delete workspace and execute above script again, update/reinstall caTools). You could try ls() and see if X is present. Maybe there is a problem with x and X, but it is correct in your presented code. - Phann
@Phann thanks for your suggestions but I'm still having problems. I have installed caTools using the packages menu in RGui. Does this mean I should delete the install.packages('caTools') command from the script - if I leave it in it complains that caTools is already in use.... - user11128
You could comment #install.packages("caTools") for convenience. With a fresh R Session (no packages loaded, empty global environment) your script seems to work exactly as you posted it at least for two people. Btw: what happens if you just call X or print(X)? - Phann