generate-rdata.R 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. print("loading data...")
  59. data <- read_csv2("../app/data/amostra.csv")
  60. brasil <- read_csv2("../app/data/brasil.csv")
  61. acidentes <- read_csv2("../app/data/acidentes.csv")
  62. #barchart data
  63. pais_bar <- read_csv2("../app/data/barchart/ac_pais_2012.csv")
  64. regioes_bar <- read_csv2("../app/data/barchart/ac_regiao_2012.csv")
  65. estados_bar <- read_csv2("../app/data/barchart/ac_uf_2012.csv")
  66. meso_bar <- read_csv2("../app/data/barchart/ac_meso_2012.csv")
  67. micro_bar <- read_csv2("../app/data/barchart/ac_micro_2012.csv")
  68. municipios_bar <- read_csv2("../app/data/barchart/ac_mun_2012.csv")
  69. #radarchart data
  70. pais_radar <- read_csv2("../app/data/radarchart/pais_by_year.csv")
  71. regioes_radar <- read_csv2("../app/data/radarchart/regiao_by_year.csv")
  72. estados_radar <- read_csv2("../app/data/radarchart/uf_by_year.csv")
  73. meso_radar <- read_csv2("../app/data/radarchart/meso_by_year.csv")
  74. micro_radar <- read_csv2("../app/data/radarchart/micro_by_year.csv")
  75. municipios_radar <- read_csv2("../app/data/radarchart/mun_by_year.csv")
  76. print("Computing scatterplot data...")
  77. sct_data <- as.matrix(data[ , c("hora_acidente", "idade_cat", "ds_agente_causador")])
  78. print("Computing choropleth map data...")
  79. choropleth_data <- read_csv2("../app/data/puf.csv")
  80. print("Computing parallel coordinates data...")
  81. parallelcoordinates_data <- read_csv2("../app/data/acidentes-sumarizado.csv")
  82. print("Computing barchart data...")
  83. pais_bar <- make_bardata(pais_bar)
  84. regioes_bar <- make_bardata2(regioes_bar)
  85. estados_bar <- make_bardata2(estados_bar)
  86. meso_bar <- make_bardata2(meso_bar)
  87. micro_bar <- make_bardata2(micro_bar)
  88. municipios_bar <- make_bardata2(municipios_bar)
  89. bar_data <- c(pais_bar, regioes_bar, estados_bar,
  90. meso_bar, micro_bar, municipios_bar)
  91. print("Computing radarchart data...")
  92. pais_radar <- make_radardata(pais_radar)
  93. regioes_radar <- make_radardata2(regioes_radar)
  94. estados_radar <- make_radardata2(estados_radar)
  95. meso_radar <- make_radardata2(meso_radar)
  96. micro_radar <- make_radardata2(micro_radar)
  97. municipios_radar <- make_radardata2(municipios_radar)
  98. radar_data <- c(pais_radar, regioes_radar, estados_radar,
  99. meso_radar, micro_radar, municipios_radar)
  100. print("Computing treemap data...")
  101. treemap_data <- makeList(unique(acidentes))
  102. print("Computing dendrogram data...")
  103. tree_data <- rsplit(unique(brasil))[[1]]
  104. print("Writing out generated R objects...")
  105. saveRDS(sct_data, "../app/data/rds/sct_data.rds")
  106. saveRDS(choropleth_data, "../app/data/rds/choropleth_data.rds")
  107. saveRDS(parallelcoordinates_data, "../app/data/rds/parallelcoordinates_data.rds")
  108. saveRDS(bar_data, "../app/data/rds/bar_data.rds")
  109. saveRDS(radar_data, "../app/data/rds/radar_data.rds")
  110. saveRDS(treemap_data, "../app/data/rds/treemap_data.rds")
  111. saveRDS(tree_data, "../app/data/rds/tree_data.rds")
  112. print("Done!")