Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

r - .png output for each row of data.frame and making .gif animation

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...