Automate an output using “while”

X<-2008
name<-"Flintshire"
maxY<-4500
while(X<2034){
png(paste("C:/PLOTS/",name, X ,".png"),width = 800, height = 600,units="px",bg="white")
plot(Persons~Age,data=subset(a,Area==name & Year==X), type='l', col="red", ylim=c(0,maxY)
,main=paste("Population Prediction: ",name," Year: ",X)
)
dev.off()
Y<-X
X<-Y+1}

once completed then animate using imagemagick

convert   -delay 20   -loop 0   Flintshire*.png   Flintshire.gif

and also some more:

#library(RODBC)
#library(lattice)

ch<-odbcConnect("cposerver")
sqlTables(ch)
c<-sqlQuery(ch, paste("select dttm ,count(*) as N" 
,"FROM MGW_AnE_BASE as a join DateTimeList as t on 1=1 and t.dttm between a.arrival_Dttm and depart_Dttm" 
,"GROUP BY t.dttm order by t.dttm")
)

str(c)

x1<-1
x2<-2016
for(j in 1:length(c$dttm)) {

png(paste("C:/PLOTS/PLOTS_", x1 ,".png")
,width = 1900, height = 600,units="px",bg="white")

plot(N ~ dttm
,data=subset(c, dttm >= c$dttm[x1] & dttm <= c$dttm[x2]) 
,type="l"
,ylim=c(0,50)
)

mtext(paste(x1))
dev.off()
x1<-x1+12
x2<-x2+12
}
dev.off()