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


resize

Use imagemagick to resize an image using a percentage:

mogrify -resize 21% *.jpg

resizes all jpegs in a directory
*WARNING* replaces original files

This next converts all the Gifs in a directory to jpegs:

mogrify -format jpg *.gif

Auto PNG of adjusted bi valued plot

##load the required bits
library(RODBC) ##for DB connection
library(doBy) ##for summaryBy
library(grDevices)## for outpur to PNG
channel<-odbcConnect("mike_db", uid="mike")
a<-sqlFetch(channel,"AnE_ind")
c<-summaryBy(wait~month_val,data=a, FUN=function(x){c(mean=mean(x),sd=sd(x),l=length(x),m=median(x))})
###########################
png(width = 750, height = 500,units="px",file="Z:/PLOTS/myplot3.png",bg="white")
par(mar=c(5, 4, 4, 5) + 0.1) ##add a margin to all subsequent charts
plot(
wait.l~month_val,data=c, type="l",lwd=1,col="blue"
,ylim=c(0,max(wait.l)+0) 
	, xlab="Date", ylab=""
	,main="YG Monthly A&E Attendance (Blue) 
	with Median Wait in minutes (Red)" 	)
par(new=T)
plot(wait.m~month_val,data=c,axes="F", type="l",lwd=1,ylim=c(0,max(wait.mean)+0)
,col="red",ylab="", xlab="")
mtext("Median Time Arrival to Discharge (mins)", col="red",4,2.4) ##"text", colour,side , offset
axis(side=4)
mtext("Monthly Attendance", col="blue",2,2.4)
axis(side=4)
dev.off()
########################################
ygts<-ts(c$wait.l,frequency=12)
plot(decompose(ygts))
########################################
ygMts<-ts(c$wait.m,frequency=12)
plot(decompose(ygMts))
########################################

sql access part 2 with boxplot and big query

##load the required bits
library(RODBC)
library(lattice)
##set up the connection
channel<-odbcConnect("mike_db", uid="mike")
##sqlTables(channel)
##a<-sqlFetch(channel,"AnE")
##b<-sqlFetch(channel,"vwAnE")
##b<-sqlQuery(channel, paste("select * from vwAnE order by loc, month_val"))
##xyplot(N~month_val|LOC,b, type="l")

##c<-sqlQuery(channel, paste("select CM,sum(numerator)"
##					,"from EP_2010_2011_ENHANCED"#
##					, "WHERE not parnt like 'z_%' "
##					," group by CM"
##					))

##execute a query for the data
e<-sqlQuery(channel, paste("select CM,aggregate_02,spect,Numerator,denominator,value,target, year_month+'-01' as year_month" 
,"from EP_2010_2011_ENHANCED"
,"where 1=1 and CM like '%e_01%'and TIME_TYPE='per-month'"
,"and parnt like 'z_t%'order by aggregate_02,spect,year_month"))
##convert the year_month value to a date

e$year_month<-as.Date(e$year_month)

##plot the lattice

xyplot(value~year_month|spect,e, type="l", cex=3)



xyplot(value+target~year_month|spect,e, type="l", size=5)