2

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.

user11128
  • 295
  • 1
  • 2
  • 12
  • Your code works for me in the sense that I get a 800 kb .gif file. – Roman Luštrik Jan 14 '17 at 08:22
  • @RomanLuštrik Does that mean there is a problem with caTools? I get absolutely nothing :( – user11128 Jan 14 '17 at 09:14
  • 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 Jan 14 '17 at 09:29
  • @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 Jan 14 '17 at 10:54
  • 1
    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 Jan 14 '17 at 11:03
  • @Phann if I replace the write.gif line of the code with just X (i.e. call X), it produces an enormous array of numbers. – user11128 Jan 15 '17 at 01:53

0 Answers0