-1

I have some trouble with generating .png from each rows of data frame.

Basically, I want to rbind each one of the row of df to coordinate_sys.

For each row of df together with coordinate_sys, a coordinate system and one a unit vector "J" should be generated like in this

Finally, after generating a .png file for each unit_vector, I would like to make .gif animation.

here is the reproducible code of my script;

library(matlib)
library(rgl)
set.seed(12)
x <- runif(10,-0.14,0.1)
y <- runif(10,-0.14,0.1)
z <-sort(runif(10,-0.9,0.9),decreasing=TRUE)
df <- data.frame(x,y,z)


rot <- function(df,out){
  coordinate_sys <- rbind(c(1,0,0),c(0,-1,0),c(0,0,1))
  vec <- rbind(coordinate_sys, unlist(df))
  rownames(vec) <- c("X", "Y", "Z", "J")
  print(vectors3d(vec, col=c(rep("black",3), "red"), lwd=2))
 out <-  png(file="example%02d.png", width=200, height=200)
  dev.off()
}

apply(df, 1,rot,out)
Community
  • 1
  • 1
Alexander
  • 4,527
  • 5
  • 51
  • 98
  • what is the point of giving downvote? – Alexander Feb 02 '16 at 01:11
  • You can try similar to [this](http://www.inside-r.org/packages/cran/rgl/docs/rgl.postscript) – akrun Feb 02 '16 at 01:18
  • @akrun your previous answer was fine except so far no rotation is needed. Only one J vector shown in each plot. thaTs all I really want. In your answer all plots are the same except rotation angle. – Alexander Feb 02 '16 at 01:20
  • Yes, that was the reason I deleted it. Some bug in the code. – akrun Feb 02 '16 at 01:21
  • @akrun I still cannot understand what to do the link you provided. My task far more different the example provided there. I'm binding rows and want to execute function for each row. the example is plotting for whole df. – Alexander Feb 02 '16 at 01:25
  • I think you can loop over the rows and export it to one of the formats mentioned in the link. – akrun Feb 02 '16 at 01:27

1 Answers1

1

This is how far I got:

rot <- function(dat, i){
  coordinate_sys <- rbind(c(1,0,0),c(0,-1,0),c(0,0,1))
  vec <- rbind(coordinate_sys, unlist(dat))
  rownames(vec) <- c("X", "Y", "Z", "J")
  open3d()
  plot3d(1, xlim = c(-1, 1), ylim = c(-1, 1), zlim = c(-1, 1), type = 'n')
  print(vectors3d(vec, col=c(rep("black",3), "red"), lwd=2))
  rgl.snapshot(paste0(letters[i], '.png'))
  rgl.close()
}

mapply(rot, split(df, 1:nrow(df)), i = 1:nrow(df))

This code will generate 10 .png images with one vector in each and a stable coordinate system. (It generates warnings though.)

Now, we should be able to make that into a .gif using the animation package, but it fails on my machine due ImageMagick not wanting to read .png files (?). Perhaps it will work on yours.

library(animation)
im.convert('*.png')
Axeman
  • 32,068
  • 8
  • 81
  • 94