Skip to content

Commit

Permalink
Merge branch 'fix_cache_bugs'
Browse files Browse the repository at this point in the history
  • Loading branch information
plablo09 committed May 19, 2021
2 parents 8182697 + 7e2a4f2 commit 8e237c3
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 60 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Imports:
leaflet.extras,
janitor,
magrittr,
memoise
memoise,
cachem
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.1.1
Expand Down
15 changes: 7 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,21 @@ RUN apt-get update && \
apt-get upgrade -y && \
apt-get clean

RUN mkdir /visualizadorIncidentesViales
RUN R -e "install.packages('devtools', repos = 'http://cran.us.r-project.org')"
RUN R -e "devtools::install_github('CentroGeo/visualizadorIncidentesViales', force=TRUE)"
ADD ./ /visualizadorIncidentesViales/
RUN R -e "devtools::install_local('/visualizadorIncidentesViales')"
#RUN R -e "devtools::install_github('CentroGeo/visualizadorIncidentesViales', force=TRUE, ref = 'fix_cache_bugs')"

###Make dir for cache
RUN mkdir ./cache_dir
# expose port
EXPOSE 3838

# run app on container start
CMD ["R", "-e", \
"options('shiny.port'=3838,shiny.host='0.0.0.0'); \
options('Actualizar_datos' = TRUE); \
options('Cache_dir'='./cache_dir');\
options('Cache_DB_dir'='./cache_dir');\
library(visualizadorIncidentesViales); \
visualizadorIncidentesViales::run_app()"]
options('Actualizar_datos' = TRUE); \
library(visualizadorIncidentesViales); \
visualizadorIncidentesViales::run_app()"]
# CMD ["R", "-e", \
# "devtools::install_github('CentroGeo/visualizadorIncidentesViales', force=TRUE); \
# options('shiny.port'=3838,shiny.host='0.0.0.0'); \
Expand Down
2 changes: 0 additions & 2 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#' @import shiny
#' @noRd
app_server <- function(input, output, session) {
########To set new upload file size
#options(shiny.maxRequestSize=510*1024^2)
actualizar <- getOption("Actualizar_datos")
output$menu <- shinydashboard::renderMenu({
shinydashboard::sidebarMenu(id = "tabs",
Expand Down
38 changes: 18 additions & 20 deletions R/mod_DBSelector.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,6 @@ filtra_datos <- function(datos_filtrados,
return(datos_filtrados)
}

# Esta sólo se encarga de que exista el objeto cache
fc <- function(cache_selected = "./cache_dir") {
dir.create(cache_selected, showWarnings = FALSE)
fc_c <- memoise::cache_filesystem(cache_selected)
print(paste("Path for DB cache Storage: ", cache_selected))
return (fc_c)
}

# Versión memoizada de filtra_datos
mem_filtra_datos <- memoise::memoise(
filtra_datos,
cache = fc(getOption(
"Cache_DB_dir",
default = "./cache_dir"
)
)
)

#' DBSelector Server Function
#'
Expand All @@ -131,10 +114,25 @@ mod_DBSelector_server <- function(input, output, session,
filtro_bd <- input$filtro_bd
filtro_intervalo <- interval_ba_rea()
filtro_lugar <- input$filtro_lugar
datos_filtrados <- mem_filtra_datos(datos_filtrados, filtro_incidente,
filtro_bd, filtro_intervalo, filtro_lugar)
datos_filtrados <- dplyr::filter(
datos_filtrados,
(datos_filtrados$timestamp > lubridate::ymd(filtro_intervalo[1])
&
datos_filtrados$timestamp < lubridate::ymd(filtro_intervalo[2])
)
)
datos_filtrados <- dplyr::filter(datos_filtrados,
fuente %in% filtro_bd) %>%
dplyr::filter(tipo_incidente %in% filtro_incidente)
### Filter by location (Alcaldias)
if (filtro_lugar != "Total Ciudad de México") {
datos_filtrados <- dplyr::filter(datos_filtrados,
nom_mun == filtro_lugar)
}
return(datos_filtrados)
})
}) %>%
bindCache(input$filtro_incidente, input$filtro_bd,
interval_ba_rea(), input$filtro_lugar)
#datafram_re <- datafram_re %>% shiny::debounce(100)
seleccion_lugar <- reactive({
input$filtro_lugar
Expand Down
2 changes: 1 addition & 1 deletion R/mod_bar.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod_bar_ui <- function(id) {
# TODO: deben venir de los datos
min = as.Date("2018-01-01", "%Y-%m-%d"),
max = as.Date("2020-09-09", "%Y-%m-%d"),
value = c(as.Date("2020-03-01", "%Y-%m-%d"),
value = c(as.Date("2019-08-01", "%Y-%m-%d"),
as.Date("2020-09-09", "%Y-%m-%d"))
)
)
Expand Down
33 changes: 8 additions & 25 deletions R/mod_graficas.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ mod_graficas_ui <- function(id) {
#'
#'@returns The render plot selected in the UI
mes_dia_graf <- function(dataframe_rec_in, input) {
cache_dir_o <- getOption("Cache_dir", default = "./cache_dir")
renderCachedPlot({
renderPlot({
if (input$tiempo_grafica == "Mensual") {
############## Mensual####################
datos <- dataframe_rec_in()
Expand Down Expand Up @@ -236,16 +235,9 @@ mes_dia_graf <- function(dataframe_rec_in, input) {

}
return(p)
},
cacheKeyExpr = list(input$Datos_grafica,
input$tiempo_grafica,
dataframe_rec_in()
),
cache = diskCache(
dir = cache_dir_o,
max_size = 20 * 1024^2
)
)
}
) %>%
bindCache(input$Datos_grafica, input$tiempo_grafica, dataframe_rec_in())
}

#' Grafica Horas Function
Expand All @@ -260,10 +252,7 @@ mes_dia_graf <- function(dataframe_rec_in, input) {
#'
#'@returns The render plot selected in the UI
horas_graf <- function(dataframe_rec_in, input) {
# renderPlot({
cache_dir_o <- getOption("Cache_dir", default = "./cache_dir")
print(cache_dir_o)
renderCachedPlot({
renderPlot({
datos <- dataframe_rec_in()
if(nrow(datos)==0){
## Grafica vacia
Expand Down Expand Up @@ -327,15 +316,9 @@ horas_graf <- function(dataframe_rec_in, input) {
panel.border = ggplot2::element_blank()
)
return(p)
},
cacheKeyExpr = list(input$tipo_grafica2,
dataframe_rec_in()
),
cache = diskCache(
dir = cache_dir_o,
max_size = 20 * 1024^2
)
)
}
) %>%
bindCache(input$tipo_grafica2, dataframe_rec_in())
}
#' graficas Server Function
#'
Expand Down
3 changes: 2 additions & 1 deletion app.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Launch the ShinyApp (Do not remove this comment)
# To deploy, run: rsconnect::deployApp()
# Or use the blue button on top of this file

#options(shiny.maxRequestSize=510*1024^2)
shinyOptions(cache = cachem::cache_disk(file.path(dirname(tempdir()), "vis-viales-cache")))
pkgload::load_all(export_all = FALSE,helpers = FALSE,attach_testthat = FALSE)
options( "golem.app.prod" = TRUE)
visualizadorIncidentesViales::run_app() # add parameters here (if any)
Expand Down
4 changes: 2 additions & 2 deletions dev/run_dev.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ options(shiny.maxRequestSize = 510 * 1024^2)
shinyOptions(cache = memoryCache(size = 20e7))

options("Actualizar_datos" = TRUE)
options("Cache_dir" = "./cache_vis")
options("Cache_DB_dir" = "./cache_vis")
options("Cache_dir" = "./cache_dir")
options("Cache_DB_dir" = "./cache_dir")

# Detach all loaded packages and clean your environment
golem::detach_all_attached()
Expand Down

0 comments on commit 8e237c3

Please sign in to comment.