generate-rdata.R 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env Rscript
  2. library(readr)
  3. library(dplyr)
  4. # TODO rename functions
  5. # This one generates a list for a tree or dendrogram
  6. rsplit <- function(x) {
  7. if(ncol(x) > 1) {
  8. listSplit <- split(x[-c(1)], x[1], drop=F)
  9. lapply(names(listSplit), function(y) {list(name = y,
  10. children = rsplit(listSplit[[y]]))})
  11. } else {
  12. lapply(seq(nrow(x[1])), function(y) {list(name = as.character(x[y,1]))})
  13. }
  14. }
  15. # This one generates a list for a treemap
  16. makeList <- function(x) {
  17. if(ncol(x) > 2) {
  18. listSplit <- split(x[-c(1)], x[1], drop=F)
  19. lapply(names(listSplit), function(y) {list(name = y,
  20. value = as.integer(listSplit[[y]][1,1]),
  21. children = makeList(listSplit[[y]][-1]))})
  22. } else {
  23. lapply(seq(nrow(x[1])), function(y) {list(name = as.character(x[y,1]),
  24. value = as.integer(x[y,2]))})
  25. }
  26. }
  27. make_bardata <- function(x) {
  28. listSplit <- split(x[-1], x[1], drop=F)
  29. lapply(names(listSplit), function(y) {
  30. list(name = y,
  31. value = as.vector(filter(x, x[1] == y)$n))
  32. })
  33. }
  34. make_bardata2 <- function(x) {
  35. apply(unique(x[c(1, 2)]), 1, function(y) {
  36. list(name = c(as.character(y[1]), as.character(y[2])),
  37. value = as.vector(filter(x, x[1] == y[1], x[2] == y[2])$n))
  38. })
  39. }
  40. row_to_list <- function(d) {
  41. apply(d, 1, function(x) {
  42. setNames(as.list(x), c("axis", "value"))
  43. })
  44. }
  45. make_radardata <- function(x) {
  46. listSplit <- split(x[-c(1)], x[1], drop=F)
  47. lapply(names(listSplit), function(y) {
  48. list(name = y,
  49. value = row_to_list(filter(x, x[1] == y)[-1]))
  50. })
  51. }
  52. make_radardata2 <- function(x) {
  53. apply(unique(x[c(1, 2)]), 1, function(y) {
  54. list(name = c(as.character(y[1]), as.character(y[2])),
  55. value = row_to_list(filter(x, x[1] == y[1], x[2] == y[2])[-c(1,2)]))
  56. })
  57. }
  58. make_hm_list <- function(x) {
  59. apply(x, 1, function(y) {
  60. list(uf = as.character(y[1]),
  61. month = as.integer(y[2]),
  62. day = as.integer(y[3]),
  63. value = as.integer(y[4]))
  64. })
  65. }
  66. print("loading data...")
  67. brasil <- read_csv2("../app/data/brasil.csv")
  68. acidentes <- read_csv2("../app/data/acidentes.csv")
  69. #barchart data
  70. pais_bar <- read_csv2("../app/data/barchart/ac_pais_2012.csv")
  71. regioes_bar <- read_csv2("../app/data/barchart/ac_regiao_2012.csv")
  72. estados_bar <- read_csv2("../app/data/barchart/ac_uf_2012.csv")
  73. meso_bar <- read_csv2("../app/data/barchart/ac_meso_2012.csv")
  74. micro_bar <- read_csv2("../app/data/barchart/ac_micro_2012.csv")
  75. municipios_bar <- read_csv2("../app/data/barchart/ac_mun_2012.csv")
  76. #radarchart data
  77. pais_radar <- read_csv2("../app/data/radarchart/pais_by_year.csv")
  78. regioes_radar <- read_csv2("../app/data/radarchart/regiao_by_year.csv")
  79. estados_radar <- read_csv2("../app/data/radarchart/uf_by_year.csv")
  80. meso_radar <- read_csv2("../app/data/radarchart/meso_by_year.csv")
  81. micro_radar <- read_csv2("../app/data/radarchart/micro_by_year.csv")
  82. municipios_radar <- read_csv2("../app/data/radarchart/mun_by_year.csv")
  83. #heatmap_table data
  84. heatmap_data_csv_2012<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2012.csv", na = "NA")
  85. heatmap_data_csv_2013<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2013.csv", na = "NA")
  86. heatmap_data_csv_2014<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2014.csv", na = "NA")
  87. heatmap_data_csv_2015<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2015.csv", na = "NA")
  88. heatmap_data_csv_2016<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2016.csv", na = "NA")
  89. heatmap_data_csv_2017<- read_csv2(file = "../app/data/heatmap/estados_mes_dia_2017.csv", na = "NA")
  90. #print("Computing scatterplot data...")
  91. #sct_data <- as.matrix(data[ , c("hora_acidente", "idade_cat", "ds_agente_causador")])
  92. print("Computing choropleth map data...")
  93. choropleth_data <- read_csv2("../app/data/puf.csv")
  94. print("Computing parallel coordinates data...")
  95. parallelcoordinates_data <- read_csv2("../app/data/acidentes-sumarizado.csv")
  96. print("Computing barchart data...")
  97. pais_bar <- make_bardata(pais_bar)
  98. regioes_bar <- make_bardata2(regioes_bar)
  99. estados_bar <- make_bardata2(estados_bar)
  100. meso_bar <- make_bardata2(meso_bar)
  101. micro_bar <- make_bardata2(micro_bar)
  102. municipios_bar <- make_bardata2(municipios_bar)
  103. bar_data <- c(pais_bar, regioes_bar, estados_bar,
  104. meso_bar, micro_bar, municipios_bar)
  105. print("Computing radarchart data...")
  106. pais_radar <- make_radardata(pais_radar)
  107. regioes_radar <- make_radardata2(regioes_radar)
  108. estados_radar <- make_radardata2(estados_radar)
  109. meso_radar <- make_radardata2(meso_radar)
  110. micro_radar <- make_radardata2(micro_radar)
  111. municipios_radar <- make_radardata2(municipios_radar)
  112. radar_data <- c(pais_radar, regioes_radar, estados_radar,
  113. meso_radar, micro_radar, municipios_radar)
  114. print("Computing treemap data...")
  115. treemap_data <- makeList(unique(acidentes))
  116. print("Computing dendrogram data...")
  117. tree_data <- rsplit(unique(brasil))[[1]]
  118. print("Reading Choropleth mesorregion csv data...")
  119. percent_est12 <- read_csv2("../app/data/percentage_meso2012.csv")
  120. percent_est13 <- read_csv2("../app/data/percentage_meso2013.csv")
  121. percent_est14 <- read_csv2("../app/data/percentage_meso2014.csv")
  122. percent_est15 <- read_csv2("../app/data/percentage_meso2015.csv")
  123. percent_est16 <- read_csv2("../app/data/percentage_meso2016.csv")
  124. percent_est17 <- read_csv2("../app/data/percentage_meso2017.csv")
  125. print("Computing heatmap_table data...")
  126. heatmap_2012_data <- make_hm_list(heatmap_data_csv_2012)
  127. heatmap_2013_data <- make_hm_list(heatmap_data_csv_2013)
  128. heatmap_2014_data <- make_hm_list(heatmap_data_csv_2014)
  129. heatmap_2015_data <- make_hm_list(heatmap_data_csv_2015)
  130. heatmap_2016_data <- make_hm_list(heatmap_data_csv_2016)
  131. heatmap_2017_data <- make_hm_list(heatmap_data_csv_2017)
  132. print("Writing out generated R objects...")
  133. #saveRDS(sct_data, "../app/data/rds/sct_data.rds")
  134. saveRDS(choropleth_data, "../app/data/rds/choropleth_data.rds")
  135. saveRDS(parallelcoordinates_data, "../app/data/rds/parallelcoordinates_data.rds")
  136. saveRDS(bar_data, "../app/data/rds/bar_data.rds")
  137. saveRDS(radar_data, "../app/data/rds/radar_data.rds")
  138. saveRDS(treemap_data, "../app/data/rds/treemap_data.rds")
  139. saveRDS(tree_data, "../app/data/rds/tree_data.rds")
  140. saveRDS(heatmap_2012_data, "../app/data/rds/heatmap_2012_data.rds")
  141. saveRDS(heatmap_2013_data, "../app/data/rds/heatmap_2013_data.rds")
  142. saveRDS(heatmap_2014_data, "../app/data/rds/heatmap_2014_data.rds")
  143. saveRDS(heatmap_2015_data, "../app/data/rds/heatmap_2015_data.rds")
  144. saveRDS(heatmap_2016_data, "../app/data/rds/heatmap_2016_data.rds")
  145. saveRDS(heatmap_2017_data, "../app/data/rds/heatmap_2017_data.rds")
  146. saveRDS(percent_est12, "../app/data/rds/percentage_meso2012.rds")
  147. saveRDS(percent_est13, "../app/data/rds/percentage_meso2013.rds")
  148. saveRDS(percent_est14, "../app/data/rds/percentage_meso2014.rds")
  149. saveRDS(percent_est15, "../app/data/rds/percentage_meso2015.rds")
  150. saveRDS(percent_est16, "../app/data/rds/percentage_meso2016.rds")
  151. saveRDS(percent_est17, "../app/data/rds/percentage_meso2017.rds")
  152. print("Done!")