-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.R
97 lines (80 loc) · 3.72 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
setwd("~/workspace/inform-mexico/")
dotenv::load_dot_env(".env")
paquetines <- c("dbrsocial","ggplot2","tidyverse","plyr","scales",
"maptools","rgdal","ggmap","gridExtra","rgdal",
"Hmisc","rgeos","sp","sf","rgeos","broom","scales",
"rangeMapper","ggmap","plotly","viridis", "DataExplorer",
"glue","data.table", "stringr","gsubfn")
no_instalados <- paquetines[!(paquetines %in% installed.packages()[,"Package"])]
if(length(no_instalados)) install.packages(no_instalados)
lapply(paquetines, library, character.only = TRUE)
### DB conections
con <- prev_connect()
# Funciones
load_query <- function(connection,schema,the_table,columns="*",options=""){
the_query <- "SELECT %s FROM %s.%s"
complete <- paste0(the_query," ",options)
schema <- deparse(substitute(schema))
the_table <- deparse(substitute(the_table))
initial <- RPostgreSQL::dbSendQuery(connection,
sprintf(complete,columns,schema,the_table))
}
load_geom <- function(connection,schema,the_table,columns="cve_mun, cve_ent, cve_muni, ", geom_col, col_shape, options=""){
geom_col <- deparse(substitute(geom_col))
schema <- deparse(substitute(schema))
the_table <- deparse(substitute(the_table))
col_shape <- deparse(substitute(col_shape))
the_query <- "SELECT %s FROM %s.%s"
geom_col_as <- sprintf("ST_AsText(%s) as geom",geom_col)
columns <- paste0(columns,geom_col_as)
complete <- paste0(the_query," ",options)
initial <- RPostgreSQL::dbSendQuery(connection,
sprintf(complete,columns,schema,the_table)) %>%
retrieve_result()
mun_shp = WKT2SpatialPolygonsDataFrame(initial, geom = geom_col, id = col_shape)
mun_df <- fortify(mun_shp, region = col_shape)
names(mun_df)[names(mun_df)=="id"] <- col_shape
return(mun_df)
}
theme_pub <- function(base_size=12, font=NA){
txt <- element_text(size = base_size+2, colour = "black", face = "plain")
bold_txt <- element_text(size = base_size+2, colour = "black", face = "bold")
theme_classic(base_size = base_size, base_family = font) +
theme(plot.title = element_text(size = 30, face = "bold"),
axis.title.x = element_text(size=20),
axis.title.y = element_text(size=20),
axis.text.x = element_text(angle = 45, size=22, hjust = 1),
axis.text.y = element_text(size=22),
legend.text = element_text(size=10))
}
quant_labels <- function(variable, no_classes=6){
quantiles <- quantile(variable,
probs = seq(0, 1, length.out = no_classes + 1),na.rm = TRUE)
labels <- c()
for(idx in 1:length(quantiles)){labels <- c(labels, paste0(round(quantiles[idx], 2)," – ", round(quantiles[idx + 1], 2))) }
labels <- labels[1:length(labels)-1]
variable_q <- cut(variable, breaks = quantiles,labels = labels, include.lowest = T)
return(variable_q)
}
create_reports <- function(df, task_name, output_dir){
df <- data.table(df)
output_file <- glue('{task_name}.html')
output_dir <- glue('{output_dir}')
create_report(data = df , output_file = output_file, output_dir = output_dir)
}
create_reports_2 <- function(df, task_name, output_dir){
df <- data.table(df)
output_file <- glue('{task_name}.html')
output_dir <- glue('{output_dir}')
create_report(data = df , output_file = output_file, output_dir = output_dir, y = "cve_muni", config = list(
"introduce" = list(),
"plot_missing" = list(),
"plot_histogram" = list(),
"plot_density" = list(),
#"plot_bar" = list("with" = "cve_muni"),
#"plot_correlation" = list("use" = "pairwise.complete.obs"),
#"plot_prcomp" = list(),
"plot_boxplot" = list("by" = "cve_muni"),
"plot_scatterplot" = list("by" = "cve_muni")
))
}