diff --git a/R/mod_current.R b/R/mod_current.R index 19ec96b..49009a2 100755 --- a/R/mod_current.R +++ b/R/mod_current.R @@ -36,7 +36,6 @@ mod_map_current <- function(input, output, session, db) { ns <- session$ns values <- reactiveValues( - current_map = NULL, current_time = NULL) # Database ---------------------------------------------------------------- @@ -56,13 +55,13 @@ mod_map_current <- function(input, output, session, db) { } # Icons ------------------------------------------------------------------- - sp_icons <- leaflet::awesomeIconList("MOCH" = leaflet::makeAwesomeIcon(icon = "star", + sp_icons <- leaflet::awesomeIconList("Mountain Chickadee" = leaflet::makeAwesomeIcon(icon = "star", marker = "green", iconColor = "white"), - "HOFI" = leaflet::makeAwesomeIcon(icon = "star", + "House Finch" = leaflet::makeAwesomeIcon(icon = "star", marker = "red", iconColor = "white"), - "DEJU" = leaflet::makeAwesomeIcon(icon = "star", + "Dark-eyed Junco" = leaflet::makeAwesomeIcon(icon = "star", marker = "darkpurple", iconColor = "white")) @@ -116,30 +115,26 @@ mod_map_current <- function(input, output, session, db) { con <- dbConnect(dbDriver("PostgreSQL"), host = db$host, port = db$port, dbname = db$name, user = db$user, password = db$pass) values$current_time <- Sys.time() + + query <- paste("SELECT raw.visits.bird_id, raw.visits.feeder_id, raw.visits.time, feeders.site_name, feeders.loc, birds.age, birds.sex, species.engl_name ", + "FROM raw.visits, feeders, birds, species", + "WHERE (raw.visits.feeder_id = feeders.feeder_id)", + "AND (birds.species = species.code)", + "AND (birds.bird_id = raw.visits.bird_id)", + "AND birds.species NOT IN ( 'XXXX' )", + "AND feeders.site_name IN ( 'Kamloops, BC' )") + query_time <- "AND raw.visits.time::timestamp > ( CURRENT_TIMESTAMP::timestamp - INTERVAL '24 hours' )" + withProgress(message = "Updating...", { suppressWarnings({ - data <- dbGetQuery(con, - statement = paste("SELECT raw.visits.bird_id, raw.visits.feeder_id, raw.visits.time, feeders.site_name, feeders.loc, birds.species, birds.age, birds.sex", - "FROM raw.visits, feeders, birds", - "WHERE (raw.visits.feeder_id = feeders.feeder_id)", - "AND (birds.bird_id = raw.visits.bird_id)", - "AND birds.species NOT IN ( 'XXXX' )", - "AND feeders.site_name IN ( 'Kamloops, BC' )", - "AND raw.visits.time::timestamp > ( CURRENT_TIMESTAMP::timestamp - INTERVAL '24 hours' )")) - - if(nrow(data) == 0) data <- dbGetQuery(con, - statement = paste("SELECT raw.visits.bird_id, raw.visits.feeder_id, raw.visits.time, feeders.site_name, feeders.loc, birds.species, birds.age, birds.sex ", - "FROM raw.visits, feeders, birds ", - "WHERE (raw.visits.feeder_id = feeders.feeder_id) ", - "AND (birds.bird_id = raw.visits.bird_id) ", - "AND birds.species NOT IN ( 'XXXX' )", - "AND feeders.site_name IN ( 'Kamloops, BC' ) ", - "ORDER BY raw.visits.time::timestamp DESC LIMIT 100")) + data <- dbGetQuery(con, statement = paste(query, query_time)) + if(nrow(data) == 0) data <- dbGetQuery(con, statement = paste(query, "ORDER BY raw.visits.time::timestamp DESC LIMIT 100")) }) dbDisconnect(con) if(nrow(data) > 0) { data <- data %>% + dplyr::rename(species = engl_name) %>% dplyr::mutate(time = lubridate::with_tz(time, tz = "UTC")) %>% load_format(., tz = "UTC", tz_disp = "America/Vancouver") %>% visits(.) %>% @@ -164,23 +159,11 @@ mod_map_current <- function(input, output, session, db) { # Map of current activity output$map_current <- renderLeaflet({ - req(current()) cat("Initializing map of current activity (", as.character(Sys.time()), ") ...\n") isolate({ d <- loggers_all %>% dplyr::filter(site_name == "Kamloops, BC") map <- map_leaflet_base(locs = d) %>% leaflet::addScaleBar(position = "bottomright") %>% - leaflet::addAwesomeMarkers(data = current(), - icon = ~sp_icons[species], - popup = ~paste0("