ReactivePoll with leaflet


#1

Hello,

I am writing a map in leaflet and everything works. Now I need to put a reactivepoll (or reactivefilereader) because I want the map to refresh every 10 minutes. The problem is this is not a local file that changes, this is a table that is connected to some sql queries via dbConnect in impala. Moreover, I don't know what to put in output because I want my leaflet but the examples on the internet ask to create a new data frame. This is how it looks :

library(leaflet) 
etc

#some queries to get my code from a database, blablabla

sql_incident_samy <- read_file("geo_cable.sql")

#some queries to join my tables

df <- addresses %>%
  mutate(cust_street_num = gsub("[^0-9].*", "", cust_street_num)) %>% 
  inner_join(incident_samy)

colnames(Zip_code_table)[colnames(Zip_code_table)=="ZIP Code"] <- "cust_city_cd"
df$cust_city_cd <- as.numeric(df$cust_city_cd)
df2 <- df %>% inner_join(Zip_code_table) 

#queries to download the map of belgium

download.file(url = 'http://biogeo.ucdavis.edu/data/diva/adm/BEL_adm.zip', 
 destfile = 'belgium.zip')
unzip(zipfile = 'belgium.zip')

belgium <- readOGR('BEL_adm0.shp')
saveRDS(belgium, "Belgium.RDS")
readRDS("Belgium.RDS")

#some transormation on the data, I made it on different level but here is one example

df_niveau1 <- df2 %>% 
  filter ((df$ci_level_3 == "WiFi") | (df$ci_level_3 == "Options / Services (VAS)") | (df$ci_level_3 == "Orange Hardware"))

adresses_test1 <- df_niveau1 %>% 
  mutate(address_niveau1 = paste(sep = ",", cust_street_num, cust_street_nm, cust_city_nm, cust_city_cd, cust_country_cd )) %>%
  select(address_niveau1)

geolocalisation_adresses_niveau1 <- geocode(adresses_test1$address_niveau1, source = "dsk")

geolocalisation_ad_niveau1 <- geolocalisation_adresses_niveau1 %>% 
  filter (geolocalisation_adresses_niveau1$lon <= 9) 

ui <- fluidPage(
  titlePanel("Cable incidents map"),
  leafletOutput("belgium", width = "100%", height = "820px")
)

#this is a code I found on the internet but that doesn't work

   server <- function(input, output, session) {
  

    
    data <- reactivePoll(10000, session,
                         # This function returns the time that log_file was last modified
                         checkFunc = function() {
                           if (file.exists(sql_incident_samy))
                             file.info(sql_incident_samy)$mtime[1]
                           else
                             ""
                         },
                         # This function returns the content of log_file
                         valueFunc = function() {
                           read.table(sql_incident_samy)
                         }
    )
  
    output$dataTable <- renderTable({
      data()
    })
}

#end of the internet code
  
  
  output$belgium <- renderLeaflet({
    m <- leaflet(belgium, options = leafletOptions(dragging = TRUE)) %>%
      addPolygons() %>%
      addProviderTiles("CartoDB") %>% 
      #clearMarkers() %>% 
      addResetMapButton() %>% 
      addCircleMarkers(lng = geolocalisation_ad_niveau1$lon, 
                       lat = geolocalisation_ad_niveau1$lat, 
                       radius = 1, opacity = 0.9, color = "#3A9D23",
                       group ="WiFi, Options / Services (VAS) or Orange Hardware",
                       label = ~paste0(df_niveau1$case_id)) %>% 
   
    m
    data()
    
  })
}

shinyApp(ui = ui, server = server)

#2

Here are some examples to help you find a solution.

This one presents several ways of updating the dashboard


One is using a reactive with invalidateLater inside to rerun the reactive expression at some point.

In this example, I use this mechanism to make a request to an API each 5 seconds and update a leaflet map. You need to use leafletProxy if you want to modify an existing leaflet map.

reactivePoll has example about SQL polling and there is also a shiny example in github repo.

Hopes it helps.