# Rscript ./Pipelines/piePipe/Components/pie/script.R 
# Work in local environment so that functions are saved with env 
local({ 
# Load module input 
assign("nonvoters", readRDS("/home/staff/paul/Research/IPC/Examples/InternetParty/Pipelines/piePipe/Components/tidy/nonvoters.rds")) 
assign("pop2013", readRDS("/home/staff/paul/Research/IPC/Examples/InternetParty/Pipelines/piePipe/Components/tidy/pop2013.rds")) 
# Set working directory 
oldwd <- setwd("./Pipelines/piePipe/Components/pie") 
# Module source 

youngNonvoters <- nonvoters$X2011[nonvoters$AgeGroup == "18 to 24"]

youngPop <- sum(pop2013$Value[pop2013$numericAge >= 18 &
                              pop2013$numericAge <= 24])
votePop <- sum(pop2013$Value[pop2013$numericAge >= 18])

youngPopNonvoters <- youngNonvoters/100*youngPop
youngNonvotersPercent <- 100*youngPopNonvoters/votePop

svg("pie.svg")
par(mar=c(0, 0, 0, 4))
pie(1, labels="")
par(new=TRUE)
pie(c(5, 95), labels=c("5% threshold", ""),
    col=c(NA, NA), border="black")
par(new=TRUE)
pie(c(youngNonvotersPercent, 100 - youngNonvotersPercent),
    labels=c("", ""),
    col=c(adjustcolor("#642C90", alpha=.5), NA), border=NA)
dev.off()
 
# Reset working directory 
setwd(oldwd) 
# Save module output 
}) # End local() 
