2

I'm working on a map project and have plotted two datasets of markers in a leaflet map (shiny app). One dataset is larger than the other, and I'm drawing great circle arcs connecting the many to few. Everything was working fine. I updated R from 3.5 to 3.6 and now I get "Error in if: missing value where TRUE/FALSE needed" which I have isolated to the for statement that runs the great circle arcs.

Hard coding values into gcIntermediate() works but then I can only have 1 fixed arc. Making a new dataframe with values works, but the data.frame built from my imported data no longer works.

# inside server.R
output$MapNet <-   renderLeaflet({
    leaflet() %>%
      addTiles(
        urlTemplate = "http://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", 
        attribution = 'Google'
      ) %>%
      setView(lng = -93.85, lat = 37.45, zoom = 5)
  })  


NetData <- data.frame(PRgps.lon = c(-83.35300,-82.52681,-82.31592,-82.91263,-83.61500,-82.73041,-82.73041,-82.73041,-82.73041,-82.73041),
                      PRgps.lat = c(42.97400, 45.46538, 45.45996, 45.40823, 45.25417, 45.51366, 45.51366, 45.51366, 45.51366, 45.51366),
                      JCgps.lon = c(-82.68287,-81.63665,-81.96928,-81.96928,-81.96928,-82.92507,-81.96928,-82.61287,-81.06040,-81.63665),
                      JCgps.lat = c(45.68083, 45.66951, 45.58777, 45.58777, 45.58777, 45.24366, 45.58777, 45.68083, 44.65540, 45.66951),
                      popup = c("A", "P", "M", "M", "J  ", "M", "M", "M", "M", "M")
                      )    

arcbuilder(NetData, MapNet)

# Inside global.R
arcbuilder <- function(NetData, MapNet){
  for (i in 1:nrow(NetData)) {

    arc <- as.data.frame(gcIntermediate(c(NetData[i,]$PRgps.lon, NetData[i,]$PRgps.lat), 
                                        c(NetData[i,]$JCgps.lon, NetData[i,]$JCgps.lat), 
                                        n = 100, addStartEnd = TRUE ))

    MapNet <- MapNet %>% 
      addPolylines(lat = arc$lat,
                   lng = arc$lon,
                   color = "green",
                   weight = 2,
                   popup = NetData[i,]$popup)

  }
}

The code above works, the data provided as NetData is the head(10) of the imported NetData. The num and chr aspects are the same as the proper file when left alone in that manor. When I don't hard enter it, and feed 100's of rows of data into the for loop I get the Warning: Error in if: missing value where TRUE/FALSE needed. More specifically I get:

Warning: Error in if: missing value where TRUE/FALSE needed
  48: .interm
  47: gcIntermediate
  45: arcbuilder [C:\Users\me\Documents\GitHub\Network_Map/global.R#193]
  44: <observer> [C:\Users\me\Documents\GitHub\Network_Map/server.R#136]
   1: runApp

Where Line #136 is where arcbuilder() is called in server.R and Line 193 is where gcIntermediate() is used inside arcbuilder() in global.R

Lastly, when running the entire NetData data.frame into the arcbuilder function with a map setup to work outside of shiny, it runs without error. I have checked the data from NAs or other anomalies and I'm reasonably confident there aren't any.

What am I missing?

Nick
  • 179
  • 1
  • 11

1 Answers1

0

I don't know what changed between 3.5 and 3.6 but, when playing around with debuging tools I discovered that the observe({}) containing the arcbuilder() call, was triggering 3 times before any data was loading in, which lead to NA's being sent to gcIntermediate().

solution for now is to nest my for loop into an if statement to make sure the data has loaded:

if (nrow(NetData) != 0) {
     for (i in 1:nrow(NetData)) {

Happy for someone to offer a more elegant solution but at least my code works again.

Nick
  • 179
  • 1
  • 11