When running following code, got error message "Invalid Parameter - -delay", can anyone help to fix the problem? Thank you.
#number of frames or plots
frames <- 50
# function for creating file name with leading zeros
# makes it easier to process them sequentially
rename <- function(x){
if (x < 10) {
return(name <- paste('000',i,'plot.png',sep=''))
}
if (x < 100 && i >= 10) {
return(name <- paste('00',i,'plot.png', sep=''))
}
if (x >= 100) {
return(name <- paste('0', i,'plot.png', sep=''))
}
}
#loop through plots
for(i in 1:frames){
name <- rename(i)
#saves the plot as a .png file in the working directory
png(name)
sd <- 10
n <- 10000
factor <- i * 2
m <- 50 + factor
x <- rnorm(n, m, sd)
hist(x,
xlim=c(0,200),
ylim=c(0,2000),
main = paste('Histogram of rnorm() n = ', n, ' mean = ', m, ' sd = ', sd),
)
dev.off()
}
#run ImageMagick
my_command <- 'convert *.png -delay 3 -loop 0 animation.gif'
system(my_command)
You didn't give detail of your environment, but from the error message I suppose that you are running on Windows. 'convert' is a built-in Windows command that has nothing to do with ImageMagick, so that is why you get the error message.
First, you can simplify the creation of png files.
#number of frames or plots
frames <- 50
png("%04dplot.png")
#loop through plots
for(i in 1:frames){
#saves the plot as a .png file in the working directory
sd <- 10
n <- 10000
factor <- i * 2
m <- 50 + factor
x <- rnorm(n, m, sd)
hist(x,
xlim=c(0,200),
ylim=c(0,2000),
main = paste('Histogram of rnorm() n = ', n, ' mean = ', m, ' sd = ', sd),
)
}
dev.off()
-i designates input.
-r defines the frame rate (Hz value) for the output.
-framerate is an input per-file option. It is meant for input formats which don't have a framerate defined, image sequences being an example.
You can play with -framerate and -r to achieve the desired animation speed.