123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- #!/usr/bin/env Rscript
- library(readr)
- library(dplyr)
- # TODO rename functions
- # This one generates a list for a tree or dendrogram
- rsplit <- function(x) {
- if(ncol(x) > 1) {
- listSplit <- split(x[-c(1)], x[1], drop=F)
- lapply(names(listSplit), function(y) {list(name = y,
- children = rsplit(listSplit[[y]]))})
- } else {
- lapply(seq(nrow(x[1])), function(y) {list(name = as.character(x[y,1]))})
- }
- }
- # This one generates a list for a treemap
- makeList <- function(x) {
- if(ncol(x) > 2) {
- listSplit <- split(x[-c(1)], x[1], drop=F)
- lapply(names(listSplit), function(y) {list(name = y,
- value = as.integer(listSplit[[y]][1,1]),
- children = makeList(listSplit[[y]][-1]))})
- } else {
- lapply(seq(nrow(x[1])), function(y) {list(name = as.character(x[y,1]),
- value = as.integer(x[y,2]))})
- }
- }
- make_bardata <- function(x) {
- listSplit <- split(x[-1], x[1], drop=F)
- lapply(names(listSplit), function(y) {
- list(name = y,
- value = as.vector(filter(x, x[1] == y)$n))
- })
- }
- make_bardata2 <- function(x) {
- apply(unique(x[c(1, 2)]), 1, function(y) {
- list(name = c(as.character(y[1]), as.character(y[2])),
- value = as.vector(filter(x, x[1] == y[1], x[2] == y[2])$n))
- })
- }
- row_to_list <- function(d) {
- apply(d, 1, function(x) {
- setNames(as.list(x), c("axis", "value"))
- })
- }
- make_radardata <- function(x) {
- listSplit <- split(x[-c(1)], x[1], drop=F)
- lapply(names(listSplit), function(y) {
- list(name = y,
- value = row_to_list(filter(x, x[1] == y)[-1]))
- })
- }
- make_radardata2 <- function(x) {
- apply(unique(x[c(1, 2)]), 1, function(y) {
- list(name = c(as.character(y[1]), as.character(y[2])),
- value = row_to_list(filter(x, x[1] == y[1], x[2] == y[2])[-c(1,2)]))
- })
- }
- print("loading data...")
- data <- read_csv2("../app/data/amostra.csv")
- brasil <- read_csv2("../app/data/brasil.csv")
- acidentes <- read_csv2("../app/data/acidentes.csv")
- #barchart data
- pais_bar <- read_csv2("../app/data/barchart/ac_pais_2012.csv")
- regioes_bar <- read_csv2("../app/data/barchart/ac_regiao_2012.csv")
- estados_bar <- read_csv2("../app/data/barchart/ac_uf_2012.csv")
- meso_bar <- read_csv2("../app/data/barchart/ac_meso_2012.csv")
- micro_bar <- read_csv2("../app/data/barchart/ac_micro_2012.csv")
- municipios_bar <- read_csv2("../app/data/barchart/ac_mun_2012.csv")
- #radarchart data
- pais_radar <- read_csv2("../app/data/radarchart/pais_by_year.csv")
- regioes_radar <- read_csv2("../app/data/radarchart/regiao_by_year.csv")
- estados_radar <- read_csv2("../app/data/radarchart/uf_by_year.csv")
- meso_radar <- read_csv2("../app/data/radarchart/meso_by_year.csv")
- micro_radar <- read_csv2("../app/data/radarchart/micro_by_year.csv")
- municipios_radar <- read_csv2("../app/data/radarchart/mun_by_year.csv")
- print("Computing scatterplot data...")
- sct_data <- as.matrix(data[ , c("hora_acidente", "idade_cat", "ds_agente_causador")])
- print("Computing choropleth map data...")
- choropleth_data <- read_csv2("../app/data/puf.csv")
- print("Computing parallel coordinates data...")
- parallelcoordinates_data <- read_csv2("../app/data/acidentes-sumarizado.csv")
- print("Computing barchart data...")
- pais_bar <- make_bardata(pais_bar)
- regioes_bar <- make_bardata2(regioes_bar)
- estados_bar <- make_bardata2(estados_bar)
- meso_bar <- make_bardata2(meso_bar)
- micro_bar <- make_bardata2(micro_bar)
- municipios_bar <- make_bardata2(municipios_bar)
- bar_data <- c(pais_bar, regioes_bar, estados_bar,
- meso_bar, micro_bar, municipios_bar)
- print("Computing radarchart data...")
- pais_radar <- make_radardata(pais_radar)
- regioes_radar <- make_radardata2(regioes_radar)
- estados_radar <- make_radardata2(estados_radar)
- meso_radar <- make_radardata2(meso_radar)
- micro_radar <- make_radardata2(micro_radar)
- municipios_radar <- make_radardata2(municipios_radar)
- radar_data <- c(pais_radar, regioes_radar, estados_radar,
- meso_radar, micro_radar, municipios_radar)
- print("Computing treemap data...")
- treemap_data <- makeList(unique(acidentes))
- print("Computing dendrogram data...")
- tree_data <- rsplit(unique(brasil))[[1]]
- print("Writing out generated R objects...")
- saveRDS(sct_data, "../app/data/rds/sct_data.rds")
- saveRDS(choropleth_data, "../app/data/rds/choropleth_data.rds")
- saveRDS(parallelcoordinates_data, "../app/data/rds/parallelcoordinates_data.rds")
- saveRDS(bar_data, "../app/data/rds/bar_data.rds")
- saveRDS(radar_data, "../app/data/rds/radar_data.rds")
- saveRDS(treemap_data, "../app/data/rds/treemap_data.rds")
- saveRDS(tree_data, "../app/data/rds/tree_data.rds")
- print("Done!")
|