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