问题
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)
回答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')
来源:https://stackoverflow.com/questions/35143269/png-output-for-each-row-of-data-frame-and-making-gif-animation