Upload 3 files
Browse files- Compute_YEI.R +54 -0
- app.R +1199 -58
- dataframe extention.R +460 -0
Compute_YEI.R
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#
|
2 |
+
inspecciones.GROUP_BY.compute_yei=function(inspecciones,
|
3 |
+
GROUP_BY=c(
|
4 |
+
"ano" ,
|
5 |
+
"mes",
|
6 |
+
"CLASIFICAC",
|
7 |
+
"NOM_VULGAR"
|
8 |
+
)){
|
9 |
+
|
10 |
+
#
|
11 |
+
inspecciones %>%
|
12 |
+
dplyr::mutate(
|
13 |
+
groups=inspecciones %>%
|
14 |
+
dplyr::select(GROUP_BY) %>%
|
15 |
+
apply(MARGIN = 1, FUN=function(row){
|
16 |
+
paste(row, collapse=".")
|
17 |
+
})
|
18 |
+
) %>%
|
19 |
+
split(.$groups) %>%
|
20 |
+
lapply(function(sub_df){
|
21 |
+
new_df=
|
22 |
+
data.frame(
|
23 |
+
yei=sum(sub_df$CT_KG)
|
24 |
+
)
|
25 |
+
for (groupping_factor in GROUP_BY){
|
26 |
+
new_df[[groupping_factor]]=dplyr::first(sub_df[[groupping_factor]])
|
27 |
+
}
|
28 |
+
new_df
|
29 |
+
}) %>%
|
30 |
+
bind_rows() %>%
|
31 |
+
Dataframe.order(
|
32 |
+
setdiff(names(.), "yei")
|
33 |
+
)
|
34 |
+
}
|
35 |
+
|
36 |
+
|
37 |
+
# upstream
|
38 |
+
#
|
39 |
+
# inspecciones %>%
|
40 |
+
# inspecciones.activas() %>%
|
41 |
+
# inspecciones.ensamblar_variables_de_reporte() %>%
|
42 |
+
# #
|
43 |
+
# # under test
|
44 |
+
# #
|
45 |
+
# inspecciones.GROUP_BY.compute_yei() %>%
|
46 |
+
# #
|
47 |
+
# # downstream
|
48 |
+
# #
|
49 |
+
# tabla_de_reporte.formatear_ciclo_anual_en_columnas() %>%
|
50 |
+
# View()
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
|
app.R
CHANGED
@@ -1,58 +1,1199 @@
|
|
1 |
-
library(
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
library(magrittr)
|
2 |
+
#
|
3 |
+
# 0. routing
|
4 |
+
# --------
|
5 |
+
|
6 |
+
#
|
7 |
+
#PROYECT_HOME=dirname(rstudioapi::getActiveDocumentContext()$path)
|
8 |
+
#setwd(PROYECT_HOME)
|
9 |
+
#list.files(getwd())
|
10 |
+
#
|
11 |
+
#absolute_path=function(
|
12 |
+
# APP_HOME
|
13 |
+
#){
|
14 |
+
# sprintf("%s/%s", PROYECT_HOME,APP_HOME)
|
15 |
+
#}
|
16 |
+
#absolute_path("datasource inspecciones")
|
17 |
+
#
|
18 |
+
|
19 |
+
#setwd(absolute_path("assets"))
|
20 |
+
#unserialized_logo=imager::load.image("logo.png")
|
21 |
+
|
22 |
+
#
|
23 |
+
# 1.0 dependencias
|
24 |
+
# --------
|
25 |
+
|
26 |
+
#
|
27 |
+
library(shiny)
|
28 |
+
options(shiny.maxRequestSize=30*1024^2)
|
29 |
+
|
30 |
+
#
|
31 |
+
# 1.1 custom-dependencias
|
32 |
+
# --------
|
33 |
+
|
34 |
+
#CODE_HOME=absolute_path("code")
|
35 |
+
#setwd(CODE_HOME)
|
36 |
+
source("dataframe extention.R")
|
37 |
+
source("Compute_YEI.R")
|
38 |
+
|
39 |
+
#
|
40 |
+
# 1.1 authentication
|
41 |
+
# --------
|
42 |
+
|
43 |
+
#
|
44 |
+
talk_states=list(
|
45 |
+
"unauthenticated"="No has ingresado credenciales válidas aún",
|
46 |
+
"authenticated"="Estás autenticado y los reportes están listos para exportar"
|
47 |
+
)
|
48 |
+
#
|
49 |
+
CURRENT_STATE="unauthenticated"
|
50 |
+
EXPORT_STATE=FALSE
|
51 |
+
#
|
52 |
+
credentials.authenticate=function(
|
53 |
+
input_user,
|
54 |
+
input_pasword,
|
55 |
+
valid_credentials=data.frame(user="stockpesca", pasword="Temporal1843@!")
|
56 |
+
){
|
57 |
+
#
|
58 |
+
# cambia el estado de la aplicaci?n seg?n la validez de las credenciales
|
59 |
+
#
|
60 |
+
|
61 |
+
valid=
|
62 |
+
valid_credentials %>% dplyr::filter(user==input_user & pasword==input_pasword)
|
63 |
+
if (nrow(valid)>0)
|
64 |
+
{ .GlobalEnv[["CURRENT_STATE"]] ="authenticated"}
|
65 |
+
else {
|
66 |
+
.GlobalEnv[["CURRENT_STATE"]] ="unauthenticated"
|
67 |
+
}
|
68 |
+
|
69 |
+
print(.GlobalEnv[["CURRENT_STATE"]])
|
70 |
+
}
|
71 |
+
|
72 |
+
#
|
73 |
+
# 2.0 data-access
|
74 |
+
# --------
|
75 |
+
|
76 |
+
#
|
77 |
+
empty_table=data.frame(
|
78 |
+
data=c("No tiene permisos para ver esta tabla")
|
79 |
+
)
|
80 |
+
#
|
81 |
+
#library(dplyr)
|
82 |
+
# datasource.raw_inspecciones=function(
|
83 |
+
#
|
84 |
+
#){
|
85 |
+
# setwd(absolute_path("datasource inspecciones"))
|
86 |
+
# readxl::read_excel("TABLA MAESTRA.xlsx") %>%
|
87 |
+
#head() %>%
|
88 |
+
# as.data.frame()
|
89 |
+
#}
|
90 |
+
#datasource.raw_inspecciones() %>% View()
|
91 |
+
inspecciones.cache=NULL
|
92 |
+
#inspecciones.cache=datasource.raw_inspecciones()
|
93 |
+
|
94 |
+
|
95 |
+
#
|
96 |
+
query_data=function(
|
97 |
+
file_path
|
98 |
+
){
|
99 |
+
if(.GlobalEnv[["CURRENT_STATE"]]=="unauthenticated"){
|
100 |
+
empty_table
|
101 |
+
} else {
|
102 |
+
# datasource.raw_inspecciones()
|
103 |
+
# #
|
104 |
+
# user_input_pwd="Temporal1843@!"
|
105 |
+
# db_file_name="inspecciones.accdb"
|
106 |
+
#
|
107 |
+
# try({
|
108 |
+
# #
|
109 |
+
# setwd(absolute_path("datasource inspecciones"))
|
110 |
+
# library(odbc)
|
111 |
+
# conexion <- dbConnect(odbc::odbc(),
|
112 |
+
# .connection_string =
|
113 |
+
# sprintf(
|
114 |
+
# paste("Driver={Microsoft Access Driver (*.mdb, *.accdb)}",
|
115 |
+
# "Dbq=%s",
|
116 |
+
# "Pwd=%s",
|
117 |
+
# sep=";"), paste(paste(absolute_path("datasource inspecciones"), db_file_name, sep="/" )),user_input_pwd))
|
118 |
+
# #https://www.connectionstrings.com/access-2007/
|
119 |
+
#
|
120 |
+
# library(dplyr)
|
121 |
+
# query <-
|
122 |
+
# dbSendQuery(conexion, "SELECT * FROM inspecciones;")
|
123 |
+
# #
|
124 |
+
# dbFetch(query)
|
125 |
+
# }) %>% as.data.frame()
|
126 |
+
#
|
127 |
+
|
128 |
+
tryCatch({
|
129 |
+
data <- readxl::read_excel(file_path)
|
130 |
+
print("Archivo leído correctamente")
|
131 |
+
data
|
132 |
+
}, error = function(e) {
|
133 |
+
print("Error al leer el archivo:")
|
134 |
+
print(e$message)
|
135 |
+
data.frame(Mensaje = "Error al leer el archivo Excel")
|
136 |
+
})
|
137 |
+
}
|
138 |
+
|
139 |
+
}
|
140 |
+
|
141 |
+
#
|
142 |
+
# 2.1 reports
|
143 |
+
# --------
|
144 |
+
|
145 |
+
|
146 |
+
#
|
147 |
+
inspecciones.activas=function(
|
148 |
+
inspecciones
|
149 |
+
){
|
150 |
+
inspecciones %>%
|
151 |
+
#
|
152 |
+
dplyr::mutate(
|
153 |
+
activa=TRUE
|
154 |
+
) %>%
|
155 |
+
dplyr::filter(
|
156 |
+
activa
|
157 |
+
)
|
158 |
+
}
|
159 |
+
#
|
160 |
+
inspecciones.ensamblar_variables_de_reporte=function(
|
161 |
+
inspecciones
|
162 |
+
){
|
163 |
+
inspecciones %>%
|
164 |
+
dplyr::mutate(
|
165 |
+
ano=ANO_ZARPE,
|
166 |
+
mes=MES_ZARPE,
|
167 |
+
arte=ARTE,
|
168 |
+
sitio=SITIO,
|
169 |
+
fecha=paste(ANO_ZARPE, MES_ZARPE, DIA_ZARPE, sep="+" ),
|
170 |
+
horas_faena=HORA_FAENA
|
171 |
+
)
|
172 |
+
}
|
173 |
+
#
|
174 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas=
|
175 |
+
function(
|
176 |
+
Tabla_de_reporte
|
177 |
+
){
|
178 |
+
Tabla_de_reporte %>%
|
179 |
+
tidyr::pivot_wider(
|
180 |
+
names_from = mes,
|
181 |
+
names_prefix = "month_",
|
182 |
+
values_from = dplyr::last(names(.))
|
183 |
+
) %>%
|
184 |
+
Dataframe.order(
|
185 |
+
c( grep(names(.), pattern="^[^m]", value=TRUE),
|
186 |
+
paste("month_", 1:12, sep=""))
|
187 |
+
)
|
188 |
+
}
|
189 |
+
#
|
190 |
+
table_logic_per_index=list(
|
191 |
+
"1.1"=function(
|
192 |
+
inspecciones=inspecciones.cache
|
193 |
+
){
|
194 |
+
inspecciones %>%
|
195 |
+
inspecciones.activas() %>%
|
196 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
197 |
+
|
198 |
+
split(paste(.$ano, .$mes, .$arte)) %>%
|
199 |
+
lapply(function(sub_df){
|
200 |
+
data.frame(
|
201 |
+
ano=dplyr::first(sub_df$ano),
|
202 |
+
mes=dplyr::first(sub_df$mes),
|
203 |
+
arte=dplyr::first(sub_df$arte),
|
204 |
+
reportada="(conteo) faenas activas",
|
205 |
+
faenas_activas=sub_df %>% nrow()
|
206 |
+
)
|
207 |
+
}) %>%
|
208 |
+
bind_rows() %>%
|
209 |
+
|
210 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
211 |
+
},
|
212 |
+
"1.2"=function(
|
213 |
+
inspecciones=inspecciones.cache
|
214 |
+
){
|
215 |
+
inspecciones %>%
|
216 |
+
inspecciones.activas() %>%
|
217 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
218 |
+
|
219 |
+
split(paste(.$ano, .$mes, .$arte, .$sitio)) %>%
|
220 |
+
lapply(function(sub_df){
|
221 |
+
data.frame(
|
222 |
+
ano=dplyr::first(sub_df$ano),
|
223 |
+
mes=dplyr::first(sub_df$mes),
|
224 |
+
arte=dplyr::first(sub_df$arte),
|
225 |
+
sitio=dplyr::first(sub_df$sitio),
|
226 |
+
reportada="(conteo) faenas activas",
|
227 |
+
faenas_activas=sub_df %>% nrow()
|
228 |
+
)
|
229 |
+
}) %>%
|
230 |
+
bind_rows() %>%
|
231 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
232 |
+
},
|
233 |
+
"1.3"=function(
|
234 |
+
inspecciones=inspecciones.cache
|
235 |
+
){
|
236 |
+
inspecciones %>%
|
237 |
+
inspecciones.activas() %>%
|
238 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
239 |
+
|
240 |
+
split(paste(.$ano, .$mes, .$arte, .$AREA, .$SUBAREA)) %>%
|
241 |
+
lapply(function(sub_df){
|
242 |
+
data.frame(
|
243 |
+
ano=dplyr::first(sub_df$ano),
|
244 |
+
mes=dplyr::first(sub_df$mes),
|
245 |
+
arte=dplyr::first(sub_df$arte),
|
246 |
+
area=dplyr::first(sub_df$AREA),
|
247 |
+
subarea=dplyr::first(sub_df$SUBAREA),
|
248 |
+
reportada="(conteo) faenas activas",
|
249 |
+
faenas_activas=nrow(sub_df)
|
250 |
+
)
|
251 |
+
}) %>%
|
252 |
+
bind_rows() %>%
|
253 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
254 |
+
},
|
255 |
+
"1.4"=function(
|
256 |
+
inspecciones=inspecciones.cache
|
257 |
+
){
|
258 |
+
inspecciones %>%
|
259 |
+
inspecciones.activas() %>%
|
260 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
261 |
+
|
262 |
+
split(paste(.$ano, .$mes, .$arte)) %>%
|
263 |
+
lapply(function(sub_df){
|
264 |
+
data.frame(
|
265 |
+
ano=dplyr::first(sub_df$ano),
|
266 |
+
mes=dplyr::first(sub_df$mes),
|
267 |
+
arte=dplyr::first(sub_df$arte),
|
268 |
+
reportada="dias de actividad",
|
269 |
+
dias_actividad=length(unique(sub_df$fecha))
|
270 |
+
)
|
271 |
+
}) %>%
|
272 |
+
bind_rows() %>%
|
273 |
+
|
274 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
275 |
+
|
276 |
+
},
|
277 |
+
"1.5"=function(
|
278 |
+
inspecciones=inspecciones.cache
|
279 |
+
){
|
280 |
+
inspecciones %>%
|
281 |
+
inspecciones.activas() %>%
|
282 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
283 |
+
|
284 |
+
split(paste(.$ano, .$mes, .$arte, .$sitio)) %>%
|
285 |
+
lapply(function(sub_df){
|
286 |
+
data.frame(
|
287 |
+
ano=dplyr::first(sub_df$ano),
|
288 |
+
mes=dplyr::first(sub_df$mes),
|
289 |
+
arte=dplyr::first(sub_df$arte),
|
290 |
+
sitio=dplyr::first(sub_df$sitio),
|
291 |
+
reportada="dias de actividad",
|
292 |
+
dias_actividad=length(unique(sub_df$fecha))
|
293 |
+
)
|
294 |
+
}) %>%
|
295 |
+
bind_rows() %>%
|
296 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
297 |
+
},
|
298 |
+
"1.6"=function(
|
299 |
+
inspecciones=inspecciones.cache
|
300 |
+
){
|
301 |
+
inspecciones %>%
|
302 |
+
inspecciones.activas() %>%
|
303 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
304 |
+
|
305 |
+
split(paste(.$ano, .$mes, .$arte)) %>%
|
306 |
+
lapply(function(sub_df){
|
307 |
+
data.frame(
|
308 |
+
ano=dplyr::first(sub_df$ano),
|
309 |
+
mes=dplyr::first(sub_df$mes),
|
310 |
+
arte=dplyr::first(sub_df$arte),
|
311 |
+
reportada="(promedio) de las horas de faena x (1.3)",
|
312 |
+
horas_de_faena=round(mean(sub_df$horas_faena),3)*(1.3)
|
313 |
+
)
|
314 |
+
}) %>%
|
315 |
+
bind_rows() %>%
|
316 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
317 |
+
},
|
318 |
+
"1.7"=function(
|
319 |
+
inspecciones=inspecciones.cache
|
320 |
+
){
|
321 |
+
inspecciones %>%
|
322 |
+
inspecciones.activas() %>%
|
323 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
324 |
+
|
325 |
+
split(paste(.$ano, .$mes, .$arte, .$AREA, .$SUBAREA )) %>%
|
326 |
+
lapply(function(sub_df){
|
327 |
+
data.frame(
|
328 |
+
ano=dplyr::first(sub_df$ano),
|
329 |
+
mes=dplyr::first(sub_df$mes),
|
330 |
+
arte=dplyr::first(sub_df$arte),
|
331 |
+
area=dplyr::first(sub_df$AREA),
|
332 |
+
subarea=dplyr::first(sub_df$SUBAREA),
|
333 |
+
reportada="(promedio) de las horas de faena x (1.3)",
|
334 |
+
horas_de_faena=round(mean(sub_df$horas_faena),3)*(1.3)
|
335 |
+
)
|
336 |
+
}) %>%
|
337 |
+
bind_rows() %>%
|
338 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
339 |
+
}
|
340 |
+
)
|
341 |
+
|
342 |
+
#
|
343 |
+
#setwd(CODE_HOME)
|
344 |
+
source("Compute_YEI.R")
|
345 |
+
#
|
346 |
+
table_logic_per_index[["2.1"]]=
|
347 |
+
function(
|
348 |
+
inspecciones=inspecciones.cache
|
349 |
+
){
|
350 |
+
inspecciones %>%
|
351 |
+
inspecciones.activas() %>%
|
352 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
353 |
+
inspecciones.GROUP_BY.compute_yei(
|
354 |
+
GROUP_BY=c(
|
355 |
+
"ano" ,
|
356 |
+
"mes",
|
357 |
+
"CLASIFICAC",
|
358 |
+
"NOM_VULGAR"
|
359 |
+
)
|
360 |
+
) %>%
|
361 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
362 |
+
}
|
363 |
+
#
|
364 |
+
#
|
365 |
+
table_logic_per_index[["2.2"]]=
|
366 |
+
function(
|
367 |
+
inspecciones=inspecciones.cache
|
368 |
+
){
|
369 |
+
inspecciones %>%
|
370 |
+
inspecciones.activas() %>%
|
371 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
372 |
+
inspecciones.GROUP_BY.compute_yei(
|
373 |
+
GROUP_BY=c(
|
374 |
+
"ano",
|
375 |
+
"mes",
|
376 |
+
"arte" ,
|
377 |
+
"GRUPO",
|
378 |
+
"SUBGRUPO"
|
379 |
+
)
|
380 |
+
) %>%
|
381 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
382 |
+
}
|
383 |
+
#
|
384 |
+
table_logic_per_index[["2.3"]]=
|
385 |
+
function(
|
386 |
+
inspecciones=inspecciones.cache
|
387 |
+
){
|
388 |
+
inspecciones %>%
|
389 |
+
inspecciones.activas() %>%
|
390 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
391 |
+
inspecciones.GROUP_BY.compute_yei(
|
392 |
+
GROUP_BY=c(
|
393 |
+
"ano",
|
394 |
+
"mes",
|
395 |
+
|
396 |
+
"arte",
|
397 |
+
"sitio"
|
398 |
+
|
399 |
+
|
400 |
+
)
|
401 |
+
) %>%
|
402 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
403 |
+
}
|
404 |
+
#
|
405 |
+
table_logic_per_index[["2.4"]]=
|
406 |
+
function(
|
407 |
+
inspecciones=inspecciones.cache
|
408 |
+
){
|
409 |
+
inspecciones %>%
|
410 |
+
inspecciones.activas() %>%
|
411 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
412 |
+
inspecciones.GROUP_BY.compute_yei(
|
413 |
+
GROUP_BY=c(
|
414 |
+
"ano",
|
415 |
+
"mes",
|
416 |
+
|
417 |
+
"arte",
|
418 |
+
"SUBGRUPO",
|
419 |
+
"NOM_VULGAR"
|
420 |
+
|
421 |
+
|
422 |
+
)
|
423 |
+
) %>%
|
424 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
425 |
+
}
|
426 |
+
#
|
427 |
+
#
|
428 |
+
table_logic_per_index[["2.5"]]=
|
429 |
+
function(
|
430 |
+
inspecciones=inspecciones.cache
|
431 |
+
){
|
432 |
+
inspecciones %>%
|
433 |
+
inspecciones.activas() %>%
|
434 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
435 |
+
inspecciones.GROUP_BY.compute_yei(
|
436 |
+
GROUP_BY=c(
|
437 |
+
"ano",
|
438 |
+
"mes",
|
439 |
+
|
440 |
+
|
441 |
+
"arte",
|
442 |
+
"SUBGRUPO",
|
443 |
+
"NOM_VULGAR",
|
444 |
+
"AREA"
|
445 |
+
)
|
446 |
+
) %>%
|
447 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
448 |
+
}
|
449 |
+
#
|
450 |
+
table_logic_per_index[["2.6"]]=
|
451 |
+
function(
|
452 |
+
inspecciones=inspecciones.cache
|
453 |
+
){
|
454 |
+
inspecciones %>%
|
455 |
+
inspecciones.activas() %>%
|
456 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
457 |
+
inspecciones.GROUP_BY.compute_yei(
|
458 |
+
GROUP_BY=c(
|
459 |
+
"ano",
|
460 |
+
"mes",
|
461 |
+
|
462 |
+
"arte",
|
463 |
+
"METODO",
|
464 |
+
|
465 |
+
"GRUPO",
|
466 |
+
"SUBGRUPO",
|
467 |
+
"NOM_VULGAR"
|
468 |
+
)
|
469 |
+
) %>%
|
470 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
471 |
+
}
|
472 |
+
#
|
473 |
+
table_logic_per_index[["2.7"]]=
|
474 |
+
function(
|
475 |
+
inspecciones=inspecciones.cache
|
476 |
+
){
|
477 |
+
inspecciones %>%
|
478 |
+
inspecciones.activas() %>%
|
479 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
480 |
+
inspecciones.GROUP_BY.compute_yei(
|
481 |
+
GROUP_BY=c(
|
482 |
+
"ano",
|
483 |
+
"mes",
|
484 |
+
|
485 |
+
"arte",
|
486 |
+
"METODO",
|
487 |
+
|
488 |
+
"GRUPO",
|
489 |
+
"SUBGRUPO",
|
490 |
+
"NOM_VULGAR",
|
491 |
+
"AREA"
|
492 |
+
|
493 |
+
)
|
494 |
+
) %>%
|
495 |
+
tabla_de_reporte.formatear_ciclo_anual_en_columnas()
|
496 |
+
}
|
497 |
+
#
|
498 |
+
|
499 |
+
#
|
500 |
+
#setwd(CODE_HOME)
|
501 |
+
source("Compute_YEI.R")
|
502 |
+
#
|
503 |
+
inspecciones.ensamblar_captura_diaria=
|
504 |
+
function(
|
505 |
+
inspecciones=inspecciones.cache
|
506 |
+
){
|
507 |
+
inspecciones %>%
|
508 |
+
|
509 |
+
inspecciones.activas() %>%
|
510 |
+
inspecciones.ensamblar_variables_de_reporte() %>%
|
511 |
+
|
512 |
+
dplyr::transmute(
|
513 |
+
captura_total=CT_KG,
|
514 |
+
|
515 |
+
dia_zarpe=paste(ANO_ZARPE, MES_ZARPE , DIA_ZARPE, sep="/" ),
|
516 |
+
dia_arribo=paste(ANO_ARRIBO, MES_ARRIBO, DIA_ARRIBO, sep="/" ),
|
517 |
+
|
518 |
+
num_pescadores=PESCADORES,
|
519 |
+
|
520 |
+
arte_pesca=ARTE,
|
521 |
+
grupo=GRUPO
|
522 |
+
) %>%
|
523 |
+
dplyr::mutate(
|
524 |
+
dia_zarpe=as.Date(dia_zarpe),
|
525 |
+
dia_arribo=as.Date(dia_arribo)
|
526 |
+
) %>%
|
527 |
+
dplyr::mutate(
|
528 |
+
num_dias=as.numeric((dia_arribo-dia_zarpe)+1)
|
529 |
+
) %>%
|
530 |
+
dplyr::mutate(
|
531 |
+
captura_diaria=round( (captura_total/num_dias),4),
|
532 |
+
captura_diaria_por_pescador=round( ( (captura_total/num_dias)/num_pescadores ),4),
|
533 |
+
)
|
534 |
+
}
|
535 |
+
#
|
536 |
+
table_logic_per_index[["3.1"]]=
|
537 |
+
function(
|
538 |
+
inspecciones=inspecciones.cache
|
539 |
+
){
|
540 |
+
inspecciones %>%
|
541 |
+
inspecciones.ensamblar_captura_diaria() %>%
|
542 |
+
split(paste(.$arte_pesca,.$grupo)) %>%
|
543 |
+
lapply(function(sub_df){
|
544 |
+
data.frame(
|
545 |
+
|
546 |
+
mean_captura_diaria=round(mean(sub_df$captura_diaria, na.rm=TRUE),3),
|
547 |
+
sd_captura_diaria=round(sd(sub_df$captura_diaria, na.rm=TRUE), 3),
|
548 |
+
|
549 |
+
#IC_captura_diaria_lower=,
|
550 |
+
#IC_captura_diaria_lower=,
|
551 |
+
|
552 |
+
arte_pesca=dplyr::first(sub_df$arte_pesca),
|
553 |
+
grupo=dplyr::first(sub_df$grupo)
|
554 |
+
)
|
555 |
+
}) %>%
|
556 |
+
bind_rows()
|
557 |
+
}
|
558 |
+
#
|
559 |
+
#
|
560 |
+
table_logic_per_index[["3.2"]]=
|
561 |
+
function(
|
562 |
+
inspecciones=inspecciones.cache
|
563 |
+
){
|
564 |
+
inspecciones %>%
|
565 |
+
inspecciones.ensamblar_captura_diaria() %>%
|
566 |
+
split(paste(.$arte_pesca,.$grupo)) %>%
|
567 |
+
lapply(function(sub_df){
|
568 |
+
data.frame(
|
569 |
+
|
570 |
+
mean_captura_diaria_por_pescado=round(mean(sub_df$captura_diaria_por_pescador, na.rm=TRUE),3),
|
571 |
+
sd_captura_diaria_por_pescado=round(sd(sub_df$captura_diaria_por_pescador, na.rm=TRUE), 3),
|
572 |
+
|
573 |
+
#IC_captura_diaria_lower=,
|
574 |
+
#IC_captura_diaria_lower=,
|
575 |
+
|
576 |
+
arte_pesca=dplyr::first(sub_df$arte_pesca),
|
577 |
+
grupo=dplyr::first(sub_df$grupo)
|
578 |
+
)
|
579 |
+
}) %>%
|
580 |
+
bind_rows()
|
581 |
+
}
|
582 |
+
|
583 |
+
|
584 |
+
table_logic_per_index[["3.3"]]=
|
585 |
+
function(
|
586 |
+
inspecciones=inspecciones.cache
|
587 |
+
){
|
588 |
+
inspecciones %>%
|
589 |
+
inspecciones.ensamblar_captura_diaria() %>%
|
590 |
+
dplyr::mutate(
|
591 |
+
captura_diaria_por_pescador_por_hora = captura_diaria_por_pescador / 24 # Converting to hourly rate
|
592 |
+
) %>%
|
593 |
+
split(paste(.$arte_pesca,.$grupo)) %>%
|
594 |
+
lapply(function(sub_df){
|
595 |
+
data.frame(
|
596 |
+
mean_captura_diaria_por_pescador_por_hora=round(mean(sub_df$captura_diaria_por_pescador_por_hora, na.rm=TRUE),3),
|
597 |
+
sd_captura_diaria_por_pescador_por_hora=round(sd(sub_df$captura_diaria_por_pescador_por_hora, na.rm=TRUE), 3),
|
598 |
+
|
599 |
+
arte_pesca=dplyr::first(sub_df$arte_pesca),
|
600 |
+
grupo=dplyr::first(sub_df$grupo)
|
601 |
+
)
|
602 |
+
}) %>%
|
603 |
+
bind_rows()
|
604 |
+
}
|
605 |
+
#
|
606 |
+
fetch_table_per_index=function(
|
607 |
+
index=1.1
|
608 |
+
){
|
609 |
+
table_logic_per_index[[index]]()
|
610 |
+
}
|
611 |
+
#
|
612 |
+
table.preview=function(
|
613 |
+
table,
|
614 |
+
max_rows=15,
|
615 |
+
max_cols=8
|
616 |
+
){
|
617 |
+
tryCatch({
|
618 |
+
table[1:min(max_rows, nrow(table) ), 1:min(max_cols, ncol(nrow(table)))]
|
619 |
+
},
|
620 |
+
error = function(e){empty_table})
|
621 |
+
}
|
622 |
+
|
623 |
+
# 5. exportar a un reporte ?nico
|
624 |
+
#
|
625 |
+
|
626 |
+
# 6. interfaz del usuario exportar a un reporte ?nico
|
627 |
+
#
|
628 |
+
|
629 |
+
#setwd(PROYECT_HOME)
|
630 |
+
#
|
631 |
+
library(shiny)
|
632 |
+
ui = fluidPage(
|
633 |
+
|
634 |
+
|
635 |
+
column(3,
|
636 |
+
fluidRow(
|
637 |
+
wellPanel(
|
638 |
+
shiny::HTML("<p> Bienvenido a la <strong> aplicación pesquera </strong>. Escoja una acción </p>")
|
639 |
+
)),
|
640 |
+
fluidRow(
|
641 |
+
wellPanel(
|
642 |
+
shiny::tags$strong("Credenciales"),
|
643 |
+
shiny::textInput("usuario",
|
644 |
+
label=NULL,
|
645 |
+
placeholder = "inserte usuario",
|
646 |
+
width='100%'),
|
647 |
+
shiny::passwordInput("contrasena",
|
648 |
+
label=NULL,
|
649 |
+
placeholder = "inserte contrasena",
|
650 |
+
width='100%')
|
651 |
+
# shiny::actionButton("enviar_credenciales",
|
652 |
+
# "Enviar credenciales",
|
653 |
+
# width='100%')
|
654 |
+
)),
|
655 |
+
|
656 |
+
fluidRow(
|
657 |
+
wellPanel(
|
658 |
+
|
659 |
+
fileInput('main_file_input', 'Seleccione un archivo desde su computador',
|
660 |
+
accept = c(".xlsx")
|
661 |
+
))),
|
662 |
+
|
663 |
+
fluidRow(
|
664 |
+
wellPanel(
|
665 |
+
|
666 |
+
shiny::tags$strong("Reportes"),
|
667 |
+
shiny::tags$br(),
|
668 |
+
shiny::actionButton("exportar_reportes",
|
669 |
+
"Exportar reportes",
|
670 |
+
width='100%'),
|
671 |
+
textOutput("export_result")
|
672 |
+
),
|
673 |
+
plotOutput(
|
674 |
+
"logo",
|
675 |
+
width = "100%",
|
676 |
+
height = "200px")
|
677 |
+
),
|
678 |
+
|
679 |
+
), # column,
|
680 |
+
|
681 |
+
column(12-4,
|
682 |
+
|
683 |
+
offset = 1,
|
684 |
+
|
685 |
+
fluidRow(wellPanel(
|
686 |
+
shiny::tags$strong("Mensajes"),
|
687 |
+
textOutput("messages"))),
|
688 |
+
|
689 |
+
|
690 |
+
shiny::tags$strong("Pre-visualización de reportes"),
|
691 |
+
|
692 |
+
tabsetPanel(
|
693 |
+
|
694 |
+
tabPanel("0. Tabla maestra",
|
695 |
+
|
696 |
+
div(shiny::tableOutput("0_tabla_maestra"),
|
697 |
+
style = "font-size:50%")),
|
698 |
+
|
699 |
+
tabPanel("1. Esfuerzo",
|
700 |
+
|
701 |
+
tabsetPanel(
|
702 |
+
|
703 |
+
tabPanel(
|
704 |
+
"1.1",
|
705 |
+
div(shiny::tableOutput("1.1")),
|
706 |
+
style = "font-size:50%"),
|
707 |
+
|
708 |
+
tabPanel(
|
709 |
+
"1.2",
|
710 |
+
div(shiny::tableOutput("1.2")),
|
711 |
+
style = "font-size:50%"),
|
712 |
+
|
713 |
+
tabPanel(
|
714 |
+
"1.3",
|
715 |
+
div(shiny::tableOutput("1.3")),
|
716 |
+
style = "font-size:50%"
|
717 |
+
),
|
718 |
+
|
719 |
+
tabPanel(
|
720 |
+
"1.4",
|
721 |
+
div(shiny::tableOutput("1.4")),
|
722 |
+
style = "font-size:50%"),
|
723 |
+
|
724 |
+
tabPanel(
|
725 |
+
"1.5",
|
726 |
+
div(shiny::tableOutput("1.5")),
|
727 |
+
style = "font-size:50%"),
|
728 |
+
|
729 |
+
tabPanel(
|
730 |
+
"1.6",
|
731 |
+
div(shiny::tableOutput("1.6")),
|
732 |
+
style = "font-size:50%"),
|
733 |
+
|
734 |
+
tabPanel(
|
735 |
+
"1.7",
|
736 |
+
div(shiny::tableOutput("1.7")),
|
737 |
+
style = "font-size:50%")
|
738 |
+
)),
|
739 |
+
|
740 |
+
tabPanel("2. Captura total",
|
741 |
+
|
742 |
+
tabsetPanel(
|
743 |
+
|
744 |
+
tabPanel(
|
745 |
+
"2.1",
|
746 |
+
div(shiny::tableOutput("2.1")),
|
747 |
+
style = "font-size:50%"),
|
748 |
+
|
749 |
+
tabPanel(
|
750 |
+
"2.2",
|
751 |
+
div(shiny::tableOutput("2.2")),
|
752 |
+
style = "font-size:50%"),
|
753 |
+
|
754 |
+
tabPanel(
|
755 |
+
"2.3",
|
756 |
+
div(shiny::tableOutput("2.3")),
|
757 |
+
style = "font-size:50%"),
|
758 |
+
|
759 |
+
tabPanel(
|
760 |
+
"2.4",
|
761 |
+
div(shiny::tableOutput("2.4")),
|
762 |
+
style = "font-size:50%"),
|
763 |
+
|
764 |
+
tabPanel(
|
765 |
+
"2.5",
|
766 |
+
div(shiny::tableOutput("2.5")),
|
767 |
+
style = "font-size:50%"),
|
768 |
+
|
769 |
+
tabPanel(
|
770 |
+
"2.6",
|
771 |
+
div(shiny::tableOutput("2.6")),
|
772 |
+
style = "font-size:50%"),
|
773 |
+
|
774 |
+
tabPanel(
|
775 |
+
"2.7",
|
776 |
+
div(shiny::tableOutput("2.7")),
|
777 |
+
style = "font-size:50%")
|
778 |
+
)),
|
779 |
+
|
780 |
+
tabPanel("3. CPUE",
|
781 |
+
|
782 |
+
tabsetPanel(
|
783 |
+
|
784 |
+
tabPanel(
|
785 |
+
"3.1",
|
786 |
+
div(shiny::tableOutput("3.1")),
|
787 |
+
style = "font-size:50%"),
|
788 |
+
|
789 |
+
tabPanel(
|
790 |
+
"3.2",
|
791 |
+
div(shiny::tableOutput("3.2")),
|
792 |
+
style = "font-size:50%"),
|
793 |
+
|
794 |
+
tabPanel(
|
795 |
+
"3.3",
|
796 |
+
div(shiny::tableOutput("3.3")),
|
797 |
+
style = "font-size:50%")))
|
798 |
+
|
799 |
+
# )),
|
800 |
+
)))
|
801 |
+
|
802 |
+
|
803 |
+
#
|
804 |
+
index.managed_table=function(
|
805 |
+
index,
|
806 |
+
input
|
807 |
+
){
|
808 |
+
if(is.null(input$main_file_input) || CURRENT_STATE=="unauthenticated"){
|
809 |
+
empty_table
|
810 |
+
} else {
|
811 |
+
tryCatch({
|
812 |
+
fetch_table_per_index(index) %>%
|
813 |
+
table.preview()
|
814 |
+
}, error = function(e) {
|
815 |
+
empty_table
|
816 |
+
})
|
817 |
+
}
|
818 |
+
}
|
819 |
+
|
820 |
+
|
821 |
+
# server
|
822 |
+
server = function(input, output) {
|
823 |
+
|
824 |
+
#
|
825 |
+
usuario=reactive({input$usuario})
|
826 |
+
contrasena=reactive({input$contrasena})
|
827 |
+
exportar=reactive({input$exportar_reportes})
|
828 |
+
exportado=reactiveValues(
|
829 |
+
se_exporto=FALSE
|
830 |
+
)
|
831 |
+
actualizar_mensaje_exportado=reactive({
|
832 |
+
exportado
|
833 |
+
})
|
834 |
+
|
835 |
+
#
|
836 |
+
output$"0_tabla_maestra" = renderTable({
|
837 |
+
|
838 |
+
credentials.authenticate(input$usuario, input$contrasena)
|
839 |
+
print("Autenticado")
|
840 |
+
|
841 |
+
file_path_ = input$main_file_input$datapath
|
842 |
+
|
843 |
+
print("Path")
|
844 |
+
print(file_path_)
|
845 |
+
|
846 |
+
data <- query_data(file_path = file_path_)
|
847 |
+
#print(paste("Tabla maestra data:", data)) # debug statement
|
848 |
+
inspecciones.cache <<- data
|
849 |
+
loaded_data(data)
|
850 |
+
data %>%
|
851 |
+
table.preview()
|
852 |
+
|
853 |
+
},
|
854 |
+
striped = TRUE,
|
855 |
+
hover=TRUE,
|
856 |
+
bordered=TRUE,
|
857 |
+
width='100%',
|
858 |
+
spacing="s"
|
859 |
+
)
|
860 |
+
# Crear un reactive value para los datos cargados
|
861 |
+
loaded_data <- reactiveVal(NULL)
|
862 |
+
|
863 |
+
# Observar cambios en el archivo cargado
|
864 |
+
observeEvent(input$main_file_input, {
|
865 |
+
file_path_ <- input$main_file_input$datapath
|
866 |
+
data <- query_data(file_path = file_path_)
|
867 |
+
inspecciones.cache <<- data
|
868 |
+
loaded_data(data)
|
869 |
+
})
|
870 |
+
|
871 |
+
authenticated <- reactive({
|
872 |
+
credentials.authenticate(input$usuario, input$contrasena)
|
873 |
+
return(.GlobalEnv[["CURRENT_STATE"]] == "authenticated")
|
874 |
+
})
|
875 |
+
|
876 |
+
output$"1.1" = renderTable({
|
877 |
+
if (authenticated()) {
|
878 |
+
print(paste("User is authenticated, showing results..."))
|
879 |
+
data <- fetch_table_per_index("1.1")
|
880 |
+
data
|
881 |
+
} else {
|
882 |
+
print(paste("User is not authenticated, returning error message..."))
|
883 |
+
"No tiene permisos para ver esta tabla"
|
884 |
+
}
|
885 |
+
},
|
886 |
+
striped = TRUE,
|
887 |
+
hover = TRUE,
|
888 |
+
bordered = TRUE,
|
889 |
+
width = '100%',
|
890 |
+
spacing = "s"
|
891 |
+
)
|
892 |
+
output$"1.2" = renderTable({
|
893 |
+
if (authenticated()) {
|
894 |
+
print(paste("User is authenticated, showing results..."))
|
895 |
+
data <- fetch_table_per_index("1.2")
|
896 |
+
data
|
897 |
+
} else {
|
898 |
+
print(paste("User is not authenticated, returning error message..."))
|
899 |
+
"No tiene permisos para ver esta tabla"
|
900 |
+
}
|
901 |
+
},
|
902 |
+
striped = TRUE,
|
903 |
+
hover=TRUE,
|
904 |
+
bordered=TRUE,
|
905 |
+
width='100%',
|
906 |
+
spacing="s"
|
907 |
+
)
|
908 |
+
|
909 |
+
output$"1.3" = renderTable({
|
910 |
+
if (authenticated()) {
|
911 |
+
print(paste("User is authenticated, showing results..."))
|
912 |
+
data <- fetch_table_per_index("1.3")
|
913 |
+
data
|
914 |
+
} else {
|
915 |
+
print(paste("User is not authenticated, returning error message..."))
|
916 |
+
"No tiene permisos para ver esta tabla"
|
917 |
+
}
|
918 |
+
},
|
919 |
+
striped = TRUE,
|
920 |
+
hover=TRUE,
|
921 |
+
bordered=TRUE,
|
922 |
+
width='100%',
|
923 |
+
spacing="s"
|
924 |
+
)
|
925 |
+
|
926 |
+
output$"1.4" = renderTable({
|
927 |
+
if (authenticated()) {
|
928 |
+
print(paste("User is authenticated, showing results..."))
|
929 |
+
data <- fetch_table_per_index("1.4")
|
930 |
+
data
|
931 |
+
} else {
|
932 |
+
print(paste("User is not authenticated, returning error message..."))
|
933 |
+
"No tiene permisos para ver esta tabla"
|
934 |
+
}
|
935 |
+
},
|
936 |
+
striped = TRUE,
|
937 |
+
hover=TRUE,
|
938 |
+
bordered=TRUE,
|
939 |
+
width='100%',
|
940 |
+
spacing="s"
|
941 |
+
)
|
942 |
+
output$"1.5" = renderTable({
|
943 |
+
if (authenticated()) {
|
944 |
+
print(paste("User is authenticated, showing results..."))
|
945 |
+
data <- fetch_table_per_index("1.5")
|
946 |
+
data
|
947 |
+
} else {
|
948 |
+
print(paste("User is not authenticated, returning error message..."))
|
949 |
+
"No tiene permisos para ver esta tabla"
|
950 |
+
}
|
951 |
+
},
|
952 |
+
striped = TRUE,
|
953 |
+
hover=TRUE,
|
954 |
+
bordered=TRUE,
|
955 |
+
width='100%',
|
956 |
+
spacing="s"
|
957 |
+
)
|
958 |
+
output$"1.6" = renderTable({
|
959 |
+
if (authenticated()) {
|
960 |
+
print(paste("User is authenticated, showing results..."))
|
961 |
+
data <- fetch_table_per_index("1.6")
|
962 |
+
data
|
963 |
+
} else {
|
964 |
+
print(paste("User is not authenticated, returning error message..."))
|
965 |
+
"No tiene permisos para ver esta tabla"
|
966 |
+
}
|
967 |
+
},
|
968 |
+
striped = TRUE,
|
969 |
+
hover=TRUE,
|
970 |
+
bordered=TRUE,
|
971 |
+
width='100%',
|
972 |
+
spacing="s"
|
973 |
+
)
|
974 |
+
output$"1.7" = renderTable({
|
975 |
+
if (authenticated()) {
|
976 |
+
print(paste("User is authenticated, showing results..."))
|
977 |
+
data <- fetch_table_per_index("1.7")
|
978 |
+
data
|
979 |
+
} else {
|
980 |
+
print(paste("User is not authenticated, returning error message..."))
|
981 |
+
"No tiene permisos para ver esta tabla"
|
982 |
+
}
|
983 |
+
},
|
984 |
+
striped = TRUE,
|
985 |
+
hover=TRUE,
|
986 |
+
bordered=TRUE,
|
987 |
+
width='100%',
|
988 |
+
spacing="s"
|
989 |
+
)
|
990 |
+
output$"2.1" = renderTable({
|
991 |
+
if (authenticated()) {
|
992 |
+
print(paste("User is authenticated, showing results..."))
|
993 |
+
data <- fetch_table_per_index("2.1")
|
994 |
+
data
|
995 |
+
} else {
|
996 |
+
print(paste("User is not authenticated, returning error message..."))
|
997 |
+
"No tiene permisos para ver esta tabla"
|
998 |
+
}
|
999 |
+
},
|
1000 |
+
striped = TRUE,
|
1001 |
+
hover=TRUE,
|
1002 |
+
bordered=TRUE,
|
1003 |
+
width='100%',
|
1004 |
+
spacing="s"
|
1005 |
+
)
|
1006 |
+
output$"2.2" = renderTable({
|
1007 |
+
if (authenticated()) {
|
1008 |
+
print(paste("User is authenticated, showing results..."))
|
1009 |
+
data <- fetch_table_per_index("2.2")
|
1010 |
+
data
|
1011 |
+
} else {
|
1012 |
+
print(paste("User is not authenticated, returning error message..."))
|
1013 |
+
"No tiene permisos para ver esta tabla"
|
1014 |
+
}
|
1015 |
+
},
|
1016 |
+
striped = TRUE,
|
1017 |
+
hover=TRUE,
|
1018 |
+
bordered=TRUE,
|
1019 |
+
width='100%',
|
1020 |
+
spacing="s"
|
1021 |
+
)
|
1022 |
+
output$"2.3" = renderTable({
|
1023 |
+
if (authenticated()) {
|
1024 |
+
print(paste("User is authenticated, showing results..."))
|
1025 |
+
data <- fetch_table_per_index("2.3")
|
1026 |
+
data
|
1027 |
+
} else {
|
1028 |
+
print(paste("User is not authenticated, returning error message..."))
|
1029 |
+
"No tiene permisos para ver esta tabla"
|
1030 |
+
}
|
1031 |
+
},
|
1032 |
+
striped = TRUE,
|
1033 |
+
hover=TRUE,
|
1034 |
+
bordered=TRUE,
|
1035 |
+
width='100%',
|
1036 |
+
spacing="s"
|
1037 |
+
)
|
1038 |
+
output$"2.4" = renderTable({
|
1039 |
+
if (authenticated()) {
|
1040 |
+
print(paste("User is authenticated, showing results..."))
|
1041 |
+
data <- fetch_table_per_index("2.4")
|
1042 |
+
data
|
1043 |
+
} else {
|
1044 |
+
print(paste("User is not authenticated, returning error message..."))
|
1045 |
+
"No tiene permisos para ver esta tabla"
|
1046 |
+
}
|
1047 |
+
},
|
1048 |
+
striped = TRUE,
|
1049 |
+
hover=TRUE,
|
1050 |
+
bordered=TRUE,
|
1051 |
+
width='100%',
|
1052 |
+
spacing="s"
|
1053 |
+
)
|
1054 |
+
output$"2.5" = renderTable({
|
1055 |
+
if (authenticated()) {
|
1056 |
+
print(paste("User is authenticated, showing results..."))
|
1057 |
+
data <- fetch_table_per_index("2.5")
|
1058 |
+
data
|
1059 |
+
} else {
|
1060 |
+
print(paste("User is not authenticated, returning error message..."))
|
1061 |
+
"No tiene permisos para ver esta tabla"
|
1062 |
+
}
|
1063 |
+
},
|
1064 |
+
striped = TRUE,
|
1065 |
+
hover=TRUE,
|
1066 |
+
bordered=TRUE,
|
1067 |
+
width='100%',
|
1068 |
+
spacing="s"
|
1069 |
+
)
|
1070 |
+
output$"2.6" = renderTable({
|
1071 |
+
if (authenticated()) {
|
1072 |
+
print(paste("User is authenticated, showing results..."))
|
1073 |
+
data <- fetch_table_per_index("2.6")
|
1074 |
+
data
|
1075 |
+
} else {
|
1076 |
+
print(paste("User is not authenticated, returning error message..."))
|
1077 |
+
"No tiene permisos para ver esta tabla"
|
1078 |
+
}
|
1079 |
+
},
|
1080 |
+
striped = TRUE,
|
1081 |
+
hover=TRUE,
|
1082 |
+
bordered=TRUE,
|
1083 |
+
width='100%',
|
1084 |
+
spacing="s"
|
1085 |
+
)
|
1086 |
+
output$"2.7" = renderTable({
|
1087 |
+
if (authenticated()) {
|
1088 |
+
print(paste("User is authenticated, showing results..."))
|
1089 |
+
data <- fetch_table_per_index("2.7")
|
1090 |
+
data
|
1091 |
+
} else {
|
1092 |
+
print(paste("User is not authenticated, returning error message..."))
|
1093 |
+
"No tiene permisos para ver esta tabla"
|
1094 |
+
}
|
1095 |
+
},
|
1096 |
+
striped = TRUE,
|
1097 |
+
hover=TRUE,
|
1098 |
+
bordered=TRUE,
|
1099 |
+
width='100%',
|
1100 |
+
spacing="s"
|
1101 |
+
)
|
1102 |
+
|
1103 |
+
output$"3.1" = renderTable({
|
1104 |
+
if (authenticated()) {
|
1105 |
+
print(paste("User is authenticated, showing results..."))
|
1106 |
+
data <- fetch_table_per_index("3.1")
|
1107 |
+
data
|
1108 |
+
} else {
|
1109 |
+
print(paste("User is not authenticated, returning error message..."))
|
1110 |
+
"No tiene permisos para ver esta tabla"
|
1111 |
+
}
|
1112 |
+
},
|
1113 |
+
striped = TRUE,
|
1114 |
+
hover=TRUE,
|
1115 |
+
bordered=TRUE,
|
1116 |
+
width='100%',
|
1117 |
+
spacing="s"
|
1118 |
+
)
|
1119 |
+
output$"3.2" = renderTable({
|
1120 |
+
if (authenticated()) {
|
1121 |
+
print(paste("User is authenticated, showing results..."))
|
1122 |
+
data <- fetch_table_per_index("3.2")
|
1123 |
+
data
|
1124 |
+
} else {
|
1125 |
+
print(paste("User is not authenticated, returning error message..."))
|
1126 |
+
"No tiene permisos para ver esta tabla"
|
1127 |
+
}
|
1128 |
+
},
|
1129 |
+
striped = TRUE,
|
1130 |
+
hover=TRUE,
|
1131 |
+
bordered=TRUE,
|
1132 |
+
width='100%',
|
1133 |
+
spacing="s"
|
1134 |
+
)
|
1135 |
+
output$"3.3" = renderTable({
|
1136 |
+
if (authenticated()) {
|
1137 |
+
print(paste("User is authenticated, showing results..."))
|
1138 |
+
data <- fetch_table_per_index("3.3")
|
1139 |
+
data
|
1140 |
+
} else {
|
1141 |
+
print(paste("User is not authenticated, returning error message..."))
|
1142 |
+
"No tiene permisos para ver esta tabla"
|
1143 |
+
}
|
1144 |
+
},
|
1145 |
+
striped = TRUE,
|
1146 |
+
hover=TRUE,
|
1147 |
+
bordered=TRUE,
|
1148 |
+
width='100%',
|
1149 |
+
spacing="s"
|
1150 |
+
)
|
1151 |
+
|
1152 |
+
output$"export_result"=renderText({
|
1153 |
+
actualizar_mensaje_exportado()
|
1154 |
+
ifelse(exportado$se_exporto==TRUE, "Felicidades! El reporte fue exportado", "Aún no has exportado el reporte")
|
1155 |
+
})
|
1156 |
+
|
1157 |
+
observeEvent(input$exportar_reportes,{
|
1158 |
+
if (.GlobalEnv[["CURRENT_STATE"]] == "authenticated") {
|
1159 |
+
# Ensure the 'reportes' directory exists
|
1160 |
+
setwd(PROYECT_HOME)
|
1161 |
+
if (!dir.exists("reportes")) {
|
1162 |
+
dir.create("reportes")
|
1163 |
+
}
|
1164 |
+
setwd("reportes")
|
1165 |
+
|
1166 |
+
# Create a new workbook
|
1167 |
+
wb <- openxlsx::createWorkbook()
|
1168 |
+
|
1169 |
+
# Generate and add each report to a separate sheet
|
1170 |
+
report_indices <- c("1.1", "1.2","1.3", "1.4", "1.5", "1.6", "1.7",
|
1171 |
+
"2.1", "2.2", "2.3", "2.4", "2.5", "2.6", "2.7",
|
1172 |
+
"3.1", "3.2","3.3")
|
1173 |
+
|
1174 |
+
for (index in report_indices) {
|
1175 |
+
tryCatch({
|
1176 |
+
report_data <- fetch_table_per_index(index)
|
1177 |
+
openxlsx::addWorksheet(wb, sheetName = paste("Reporte", index))
|
1178 |
+
openxlsx::writeData(wb, sheet = paste("Reporte", index), x = report_data)
|
1179 |
+
}, error = function(e) {
|
1180 |
+
print(paste("Error generating report", index, ":", e$message))
|
1181 |
+
})
|
1182 |
+
}
|
1183 |
+
|
1184 |
+
# Save the workbook
|
1185 |
+
openxlsx::saveWorkbook(wb, "exporte_reportes.xlsx", overwrite = TRUE)
|
1186 |
+
|
1187 |
+
.GlobalEnv[["EXPORT_STATE"]] <- TRUE
|
1188 |
+
exportado$se_exporto <- TRUE
|
1189 |
+
|
1190 |
+
# Optional: Show a message or log the successful export
|
1191 |
+
print("Reports exported successfully!")
|
1192 |
+
}
|
1193 |
+
})
|
1194 |
+
|
1195 |
+
}
|
1196 |
+
|
1197 |
+
# Run the application
|
1198 |
+
shinyApp(ui = ui, server = server)
|
1199 |
+
|
dataframe extention.R
ADDED
@@ -0,0 +1,460 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Dataframe extentions
|
2 |
+
# --------
|
3 |
+
#
|
4 |
+
|
5 |
+
#
|
6 |
+
Dataframe.count_over_factor=function(
|
7 |
+
Dataframe,
|
8 |
+
counted,
|
9 |
+
normalize=FALSE
|
10 |
+
){
|
11 |
+
#
|
12 |
+
# map Dataframe to a Dataframe that counts values for counted data column
|
13 |
+
#
|
14 |
+
|
15 |
+
#
|
16 |
+
pr_as_pretty_rate=function(x){
|
17 |
+
round(x*100,3)
|
18 |
+
}
|
19 |
+
#
|
20 |
+
table(Dataframe[[counted]]) %>%
|
21 |
+
{
|
22 |
+
data.frame(
|
23 |
+
value=names(.) %>% as.character(),
|
24 |
+
count=as.numeric(.))
|
25 |
+
} %>%
|
26 |
+
{
|
27 |
+
if(normalize){
|
28 |
+
|
29 |
+
mutate(., count=
|
30 |
+
round(.[["count"]]/sum(.[["count"]])*100, 3)
|
31 |
+
)
|
32 |
+
|
33 |
+
} else {
|
34 |
+
.
|
35 |
+
}
|
36 |
+
|
37 |
+
}%>%
|
38 |
+
tidyr::pivot_wider(
|
39 |
+
names_from = value,
|
40 |
+
values_from=count
|
41 |
+
)
|
42 |
+
}
|
43 |
+
#
|
44 |
+
Dataframe.order=function(
|
45 |
+
Dataframe=db.contrataciones_normalizadas(),
|
46 |
+
order= c("tipo_contrato", "salario")
|
47 |
+
){
|
48 |
+
#
|
49 |
+
# map Dataframe to a Dataframe that counts values for counted data column
|
50 |
+
#
|
51 |
+
|
52 |
+
Dataframe %>%
|
53 |
+
{
|
54 |
+
.[,
|
55 |
+
intersect( c(order, setdiff(names(.), order) ), names(.))
|
56 |
+
]
|
57 |
+
}
|
58 |
+
}
|
59 |
+
#
|
60 |
+
#
|
61 |
+
Dataframe.map_NAS=function(
|
62 |
+
Dataframe=db.contrataciones_normalizadas(),
|
63 |
+
mapped_to=0
|
64 |
+
){
|
65 |
+
#
|
66 |
+
# map Dataframe to a Dataframe that counts values for counted data column
|
67 |
+
#
|
68 |
+
|
69 |
+
Dataframe %>%
|
70 |
+
{
|
71 |
+
.[is.na(.)
|
72 |
+
]=mapped_to;.
|
73 |
+
}
|
74 |
+
}
|
75 |
+
#
|
76 |
+
#Dataframe.map_NAS() %>% View()
|
77 |
+
#
|
78 |
+
#
|
79 |
+
basic_standardization=function(
|
80 |
+
names
|
81 |
+
){
|
82 |
+
#
|
83 |
+
tolower(names) %>%
|
84 |
+
#
|
85 |
+
iconv(.,from="UTF-8", sub="", to="ASCII//TRANSLIT")
|
86 |
+
#
|
87 |
+
}
|
88 |
+
#
|
89 |
+
stata_valid_names=function(names){
|
90 |
+
text2vec::word_tokenizer(names) %>%
|
91 |
+
sapply(., function(some_name_units){
|
92 |
+
s=paste(some_name_units, collapse="_")
|
93 |
+
s=stringr::str_replace_all(s, "[.]", "_")
|
94 |
+
substr(s, 0, 25)
|
95 |
+
}) %>% basic_standardization()
|
96 |
+
}
|
97 |
+
|
98 |
+
str_seq.replace_initial_numbers=
|
99 |
+
function(
|
100 |
+
str_seq=n
|
101 |
+
){
|
102 |
+
str_seq %>%
|
103 |
+
sapply(
|
104 |
+
function(str){
|
105 |
+
if(
|
106 |
+
stringr::str_detect(
|
107 |
+
str, "^[0-9]"
|
108 |
+
)){
|
109 |
+
str=paste( "v_",str, sep="")
|
110 |
+
};str
|
111 |
+
|
112 |
+
}
|
113 |
+
)
|
114 |
+
}
|
115 |
+
#
|
116 |
+
#str_seq.replace_initial_numbers()
|
117 |
+
|
118 |
+
#
|
119 |
+
# stata_valid_names(c("Variable_invalida1",
|
120 |
+
# "Variable//.,invalida2",
|
121 |
+
# "Variableinvalida3_con_nombre_super_largo",
|
122 |
+
# "Variable_con_acentuaciÃÂón",
|
123 |
+
# "nomnbre.invalido"
|
124 |
+
# ))
|
125 |
+
#
|
126 |
+
Dataframe.apply_valid_names_for_stata=function(
|
127 |
+
Dataframe=db.contrataciones_normalizadas()
|
128 |
+
){
|
129 |
+
names(Dataframe)=stata_valid_names(names(Dataframe)) %>%
|
130 |
+
str_seq.replace_initial_numbers
|
131 |
+
Dataframe
|
132 |
+
}
|
133 |
+
#
|
134 |
+
# Dataframe.apply_valid_names_for_stata() %>% names()
|
135 |
+
#
|
136 |
+
Dataframe.mandatory_model=function(
|
137 |
+
df,
|
138 |
+
mandatory_model
|
139 |
+
){
|
140 |
+
df %>%
|
141 |
+
dplyr::filter(
|
142 |
+
df %>%
|
143 |
+
dplyr::select( mandatory_model) %>%
|
144 |
+
apply( MARGIN=1,
|
145 |
+
function(row_data){
|
146 |
+
all(!is.na(row_data))
|
147 |
+
})
|
148 |
+
)
|
149 |
+
}
|
150 |
+
#
|
151 |
+
#
|
152 |
+
#
|
153 |
+
Dataframe.vars_as_character=function(
|
154 |
+
Dataframe
|
155 |
+
){
|
156 |
+
Dataframe %>%
|
157 |
+
lapply(function(data_col){
|
158 |
+
as.character(data_col)
|
159 |
+
}) %>%
|
160 |
+
as.data.frame()
|
161 |
+
}
|
162 |
+
#
|
163 |
+
#Dataframe.vars_as_character()
|
164 |
+
#
|
165 |
+
Dataframe.reencode=function(
|
166 |
+
Dataframe,
|
167 |
+
FileEncoding="UTF-8"
|
168 |
+
){
|
169 |
+
#
|
170 |
+
Dataframe %>% write.table("tempfile.txt")
|
171 |
+
an= read.table("tempfile.txt", encoding="UTF-8")
|
172 |
+
return(an)
|
173 |
+
}
|
174 |
+
#
|
175 |
+
#Dataframe.reencode()
|
176 |
+
#
|
177 |
+
|
178 |
+
|
179 |
+
#
|
180 |
+
Dataframe.aggregate=function(
|
181 |
+
Dataframe,
|
182 |
+
aggregated,
|
183 |
+
label,
|
184 |
+
Na.rm=TRUE
|
185 |
+
){
|
186 |
+
|
187 |
+
|
188 |
+
Dataframe %>%
|
189 |
+
{
|
190 |
+
dplyr::mutate(
|
191 |
+
.,
|
192 |
+
temp_var=
|
193 |
+
{
|
194 |
+
Dataframe %>%
|
195 |
+
dplyr::select(aggregated) %>%
|
196 |
+
apply(X=.,
|
197 |
+
MARGIN=1,
|
198 |
+
FUN=function(row_data){
|
199 |
+
sum(row_data, na.rm = Na.rm)
|
200 |
+
})
|
201 |
+
}
|
202 |
+
)
|
203 |
+
} %>%
|
204 |
+
{
|
205 |
+
.[[label]]=.[["temp_var"]];.
|
206 |
+
|
207 |
+
} %>%
|
208 |
+
dplyr::select(-"temp_var")
|
209 |
+
}
|
210 |
+
#
|
211 |
+
# Dataframe.aggregate(
|
212 |
+
# Dataframe=db.indicadores_por_oferente(),
|
213 |
+
# aggregated=c(
|
214 |
+
# "aprendizaje",
|
215 |
+
# "obra",
|
216 |
+
# "otro",
|
217 |
+
# "prest_de_servicios",
|
218 |
+
# "temporal",
|
219 |
+
# "termino_fijo",
|
220 |
+
# "termino_indefinido"),
|
221 |
+
# label="cualquier_contrato"
|
222 |
+
# ) %>% View()
|
223 |
+
|
224 |
+
|
225 |
+
#
|
226 |
+
Dataframe.complain_for_vars=function(
|
227 |
+
Dataframe,
|
228 |
+
mandatory_vars
|
229 |
+
){
|
230 |
+
stopifnot(
|
231 |
+
"some mandatory vars not found in Dataframe"=
|
232 |
+
all( (mandatory_vars %in% names(Dataframe)) )
|
233 |
+
)
|
234 |
+
#
|
235 |
+
Dataframe
|
236 |
+
}
|
237 |
+
#
|
238 |
+
# Dataframe.complain_for_vars(
|
239 |
+
# data.frame(),
|
240 |
+
# mandatory_vars="unexistent"
|
241 |
+
# )
|
242 |
+
#
|
243 |
+
# Dataframe.complain_for_vars(
|
244 |
+
# data.frame("existent1"="", "existent2"=""),
|
245 |
+
# mandatory_vars="existent1"
|
246 |
+
# )
|
247 |
+
|
248 |
+
|
249 |
+
#
|
250 |
+
Dataframe.totalize=function(
|
251 |
+
Dataframe,
|
252 |
+
i_am_not_totalizable=NaN,
|
253 |
+
Na.rm=TRUE,
|
254 |
+
group_name_col=1
|
255 |
+
){
|
256 |
+
#
|
257 |
+
an=
|
258 |
+
rbind(Dataframe,
|
259 |
+
sapply(names(Dataframe),function(data_col){
|
260 |
+
ifelse(
|
261 |
+
!(data_col %in% i_am_not_totalizable),
|
262 |
+
sum(Dataframe[[data_col]] %>% as.numeric(), na.rm=Na.rm),
|
263 |
+
"-"
|
264 |
+
)}))
|
265 |
+
if(is.numeric( group_name_col)){
|
266 |
+
an[nrow(an),group_name_col]="Totales"
|
267 |
+
}
|
268 |
+
an
|
269 |
+
}
|
270 |
+
#
|
271 |
+
#Dataframe.totalize()
|
272 |
+
|
273 |
+
#
|
274 |
+
Dataframe.prefix=function(
|
275 |
+
Dataframe,
|
276 |
+
prefix="var_"
|
277 |
+
){
|
278 |
+
names(Dataframe)=paste(prefix, names(Dataframe), sep="")
|
279 |
+
Dataframe
|
280 |
+
}
|
281 |
+
|
282 |
+
#
|
283 |
+
Dataframe.new_names=function(
|
284 |
+
Dataframe,
|
285 |
+
new_names
|
286 |
+
){
|
287 |
+
names(Dataframe)=new_names;Dataframe
|
288 |
+
}
|
289 |
+
|
290 |
+
#
|
291 |
+
Dataframe.count_values=function(
|
292 |
+
Dataframe,
|
293 |
+
counted
|
294 |
+
){
|
295 |
+
new_df=
|
296 |
+
table(Dataframe[[counted]]) %>%
|
297 |
+
as.data.frame()
|
298 |
+
new_df[[1]]=as.character(new_df[[1]])
|
299 |
+
new_df
|
300 |
+
}
|
301 |
+
|
302 |
+
#
|
303 |
+
Dataframe.insert=function(
|
304 |
+
Dataframe,
|
305 |
+
row_of_values
|
306 |
+
){
|
307 |
+
rbind(Dataframe, row_of_values)
|
308 |
+
}
|
309 |
+
|
310 |
+
Dataframe.apply_treshold=function(
|
311 |
+
Dataframe,
|
312 |
+
treshold=1
|
313 |
+
){
|
314 |
+
Dataframe[Dataframe>=treshold]=1
|
315 |
+
Dataframe[Dataframe<treshold]=0
|
316 |
+
return(Dataframe)
|
317 |
+
}
|
318 |
+
|
319 |
+
#
|
320 |
+
Dataframe.alter_table_Dataframe_add_primary_key=function(
|
321 |
+
Dataframe,
|
322 |
+
primary_key_components
|
323 |
+
){
|
324 |
+
new_df=Dataframe
|
325 |
+
new_df$primary_key=""
|
326 |
+
|
327 |
+
for (component in primary_key_components){
|
328 |
+
new_df=
|
329 |
+
new_df %>%
|
330 |
+
dplyr::mutate(
|
331 |
+
primary_key=paste(primary_key, .[[component]], sep="+" )
|
332 |
+
)
|
333 |
+
}
|
334 |
+
|
335 |
+
new_df %>%
|
336 |
+
dplyr::filter(!duplicated(primary_key)) %>%
|
337 |
+
return(new_df)
|
338 |
+
}
|
339 |
+
|
340 |
+
#
|
341 |
+
Dataframe.select_on_regex=function(
|
342 |
+
Dataframe,
|
343 |
+
selecting_regex
|
344 |
+
){
|
345 |
+
Dataframe %>%
|
346 |
+
dplyr::select(
|
347 |
+
grep(names(.), pattern=selecting_regex, value=TRUE)
|
348 |
+
) %>%
|
349 |
+
return()
|
350 |
+
}
|
351 |
+
|
352 |
+
|
353 |
+
#
|
354 |
+
Dataframe.delimite_dates=function(
|
355 |
+
Dataframe,
|
356 |
+
lower_date,
|
357 |
+
upper_date
|
358 |
+
){
|
359 |
+
Dataframe %>%
|
360 |
+
|
361 |
+
dplyr::filter(
|
362 |
+
fecha > lower_date
|
363 |
+
) %>%
|
364 |
+
dplyr::filter(
|
365 |
+
fecha < upper_date
|
366 |
+
) %>%
|
367 |
+
|
368 |
+
return()
|
369 |
+
}
|
370 |
+
|
371 |
+
|
372 |
+
#
|
373 |
+
# regex associated behavour
|
374 |
+
# --------
|
375 |
+
|
376 |
+
#
|
377 |
+
Textual_feature.basic_standardization=function(
|
378 |
+
names
|
379 |
+
){
|
380 |
+
#
|
381 |
+
tolower(names) %>%
|
382 |
+
iconv(to="ASCII//TRANSLIT")
|
383 |
+
#
|
384 |
+
}
|
385 |
+
#
|
386 |
+
i_am_my_name=function(x){
|
387 |
+
names(x)=x;x
|
388 |
+
}
|
389 |
+
#
|
390 |
+
Dataframe.expand_regex_features=function(
|
391 |
+
Dataframe,
|
392 |
+
text_source,
|
393 |
+
features=table(Dataframe[[text_source]]) %>% names() %>% i_am_my_name(),
|
394 |
+
standardization=Textual_feature.basic_standardization,
|
395 |
+
name_prefix
|
396 |
+
|
397 |
+
){
|
398 |
+
#
|
399 |
+
state_df=
|
400 |
+
Dataframe %>%
|
401 |
+
dplyr::mutate(
|
402 |
+
z_textual_source=standardization(.[[text_source]])
|
403 |
+
)
|
404 |
+
#
|
405 |
+
for (feature_name in names(features)){
|
406 |
+
state_df[[sprintf("%s%s", name_prefix, feature_name)]]=
|
407 |
+
ifelse(
|
408 |
+
stringr::str_detect(state_df$z_textual_source, pattern=features[[feature_name]]),1,0)
|
409 |
+
}
|
410 |
+
state_df %>%
|
411 |
+
dplyr::select(-"z_textual_source")
|
412 |
+
}
|
413 |
+
#
|
414 |
+
#
|
415 |
+
Dataframe.extract_regex_fields=function(
|
416 |
+
Dataframe,
|
417 |
+
text_source,
|
418 |
+
features=table(Dataframe[[text_source]]) %>% names() %>% i_am_my_name(),
|
419 |
+
standardization=Textual_feature.basic_standardization,
|
420 |
+
name_prefix
|
421 |
+
){
|
422 |
+
#
|
423 |
+
state_df=
|
424 |
+
Dataframe %>%
|
425 |
+
dplyr::mutate(
|
426 |
+
z_textual_source=standardization(.[[text_source]])
|
427 |
+
)
|
428 |
+
#
|
429 |
+
for (feature_name in names(features)){
|
430 |
+
state_df[[sprintf("%s%s", name_prefix, feature_name)]]=
|
431 |
+
stringr::str_extract(state_df$z_textual_source, pattern=features[[feature_name]])
|
432 |
+
}
|
433 |
+
state_df %>%
|
434 |
+
dplyr::select(-"z_textual_source")
|
435 |
+
}
|
436 |
+
|
437 |
+
#
|
438 |
+
Dataframe.fetch=function(
|
439 |
+
Dataframe,
|
440 |
+
fetched
|
441 |
+
){
|
442 |
+
Dataframe %>%
|
443 |
+
{
|
444 |
+
Dataframe[[fetched]]
|
445 |
+
}
|
446 |
+
}
|
447 |
+
|
448 |
+
#
|
449 |
+
Dataframe.totalize=function(
|
450 |
+
Dataframe,
|
451 |
+
lambda
|
452 |
+
){
|
453 |
+
list(
|
454 |
+
Dataframe,
|
455 |
+
lapply(Dataframe, function(col){
|
456 |
+
lambda(col)
|
457 |
+
}) %>% as.data.frame()
|
458 |
+
) %>%
|
459 |
+
bind_rows()
|
460 |
+
}
|