# Rscript ./Pipelines/reportPipe/Components/tidy/script.R 
# Work in local environment so that functions are saved with env 
local({ 
# Load module input 
assign("nvfile", "/home/staff/paul/Research/IPC/Examples/InternetParty/data/non-voters.csv") 
assign("popfile", "/home/staff/paul/Research/IPC/Examples/InternetParty/data/TABLECODE7511_Data_821b2c90-79e3-4462-9994-4ae796f6e654.csv") 
# Set working directory 
oldwd <- setwd("./Pipelines/reportPipe/Components/tidy") 
# Module source 

AgeGroups <- c("17 under", "18 to 24", "25 to 44", "45 to 64", "65 over")

nonvoters <- read.csv(nvfile, strip.white=TRUE)
nonvoters$AgeGroup <- factor(nonvoters$AgeGroup, levels=AgeGroups)

population <- read.csv(popfile)
population$numericAge <- as.numeric(gsub("Years.*", "", population$Age))
population$AgeGroup <- cut(population$numericAge,
                           c(-1, 17.5, 24.5, 44.5, 64.5, 91),
                           labels=AgeGroups)
pop2013 <- population[population$Year.at.30.June == 2013, ]
pop2013grouped <- aggregate(pop2013["Value"], list(AgeGroup=pop2013$AgeGroup),
                            sum) 
# Reset working directory 
setwd(oldwd) 
# Save module output 
saveRDS(nonvoters, "./Pipelines/reportPipe/Components/tidy/nonvoters.rds") 
saveRDS(pop2013, "./Pipelines/reportPipe/Components/tidy/pop2013.rds") 
saveRDS(pop2013grouped, "./Pipelines/reportPipe/Components/tidy/pop2013grouped.rds") 
}) # End local() 
