ReactivePoll with leaflet


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 :


#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)) %>% 

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 = '', 
 destfile = '')
unzip(zipfile = '')

belgium <- readOGR('BEL_adm0.shp')
saveRDS(belgium, "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 )) %>%

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))
                         # This function returns the content of log_file
                         valueFunc = function() {
    output$dataTable <- renderTable({

#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)) %>% 

shinyApp(ui = ui, server = server)

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.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.