Isolation and reactivity


#1

Hi I wonder if you can possibly help with an issue I have with my leaflet proxy points disappearing every time I move the map or zoom in. Th cause of which is due to the query updating based on map bounds. However I cant seem to figure out how to isolate the points on display so that they don’t disappear. I have an event based on a rendertable which triggers a query to get points to display these are then plotted in an observe function, (see code below). Id be most grateful for any help with how to isolate and stop points disappearing on zooming in or dragging the map…
Cheers.

data=dbGetQuery(con2, paste0("select * from postgresfunc('",input$dates[1]," 00:00:00','",input$dates[2]," 23:59:59',",input$map_bounds[3],",",input$map_bounds[1],",",input$map_bounds[4],",",input$map_bounds[2],")",sep=""))
output$x3 = DT::renderDataTable(data()[c(5,2)], server = FALSE, options = list(searching = FALSE))
  
  my_table=reactiveVal(structure(list(pointtime=as.Date(character()),
                                      latitude=numeric(),
                                      longitude=numeric(),
                                      distancem=numeric(),
                                      seconds=numeric(),
                                      velocityms=numeric(),
                                      lastlat=numeric(),
                                      lastlon=numeric(),     
                                      rollavg=numeric(),
                                      id=numeric(),
                                      sessionid=character()),
                                 class="data.frame"))
  
  # initiate out selection
  selection <- reactiveVal()  
  
  observeEvent(input$x3_rows_selected, ignoreNULL = F,{
    
    
    # if any from input not yet in our selection
    if(any(!input$x3_rows_selected %in% selection()))
    {
      to_add = input$x3_rows_selected[!input$x3_rows_selected %in% selection()] # which id to add?
      selection(c(selection(),to_add)) # update the reactiveVal
      dat1 =  paste0(data()[to_add, , drop = FALSE][[1]], sep = "")
      
dat2= dbGetQuery(con2, paste0("select * from postgresfunc2('",dat1,"', ",input$tpoints,", 15) where velocityms > rollavg * 0.75 and velocityms < rollavg * 1.25", sep=""))

      if (nrow(dat2) == 0){
        return(NULL)}
      dat2$id = to_add
      dat2$sessionid=dat1
      my_table(rbind(my_table(),dat2)) # update the table reactiveVal
    }
    else
    {
      # if there are rows selected that the user has deselected ...
      if(any(!selection() %in% input$x3_rows_selected ))
      {
        selection(input$x3_rows_selected) # set selection to the user input
        # remove the deselected rows from the table
        my_table(my_table()[my_table()$id %in% input$x3_rows_selected,] ) 
      }
    }
    
    
  })
  

observe({
    
    # Create map
    map <- leafletProxy("map")
    map %>% clearShapes()
    
    # Get select inputs
    shps.select <- input$tree # the function is triggered when the select option changes
   
    if (length(shps.select) > 0) {
      
   
      if ('points' %in% get_selected(shps.select)) {
        density = input$density # triggers this function when you update
        if(length(data())==0){
          return(NULL)}
        
        dat=data()
        
        dat$col = viridis(length(unique(dat$sessionid)),option="D")
        
        big_data = inner_join(my_table(), dat[c(1,6)], c("sessionid","sessionid"))
        
        if (nrow(big_data) == 0){
          return(NULL)}
        big_data = big_data[sample(nrow(big_data), density*nrow(big_data)/100,replace = TRUE, prob = NULL),]
        
        
        leafletProxy("map")  %>% addCircles(lng=~longitude, lat= ~latitude, group = "track_id",  radius=0.1,fillOpacity = 0.7, popup=paste( "<strong>time: </strong>", big_data$pointtime ), color = ~col, fillColor = ~col, data = big_data)
        
      }
      else {
        leafletProxy("map")  %>% clearGroup(group = "track_id")
      }
      
    }  
  })