1

I have been utilizing code from the answer of a previous question with great success. Last night, after successfully using the code many times I started to receive an error when trying to execute the second part of the code to access list results. The lst_elements list is not being created. This is my code.

# Run Google Distance API ALl Transit
res <- lapply(1:nrow(Lankenau), function(x) {
  google_distance(origins = c(Lankenau[x,"LAT"],Lankenau[x,"LONG"]),
                  destinations = c(Lankenau[x,"O_Lat"],Lankenau[x,"O_Long"]),
                  mode = "transit",arrival_time = time)})

lst_elements <- lapply(res, function(x){
   stats::setNames(
     cbind(
       distance_elements(x)[[1]][['duration']],
       distance_elements(x)[[1]][['distance']]
     )
     , c("duration_text", "duration_value", "distance_text", "distance_value")
   )
 })

and the error received

Error in names(object) <- nm : attempt to set an attribute on NULL 
3.
stats::setNames(cbind(distance_elements(x)[[1]][["duration"]], 
    distance_elements(x)[[1]][["distance"]]), c("duration_text", 
    "duration_value", "distance_text", "distance_value")) 
2.
FUN(X[[i]], ...) 
1.
lapply(res, function(x) {
    stats::setNames(cbind(distance_elements(x)[[1]][["duration"]], 
        distance_elements(x)[[1]][["distance"]]), c("duration_text", 
        "duration_value", "distance_text", "distance_value")) ... 

any tips would be great! I'm not sure what happened. The exact same good is still working for a different dataframe. Would this suggest that the error is stemming from the data.frame itself?

Rho
  • 21
  • 2

1 Answers1

1

Likely it is data-specific due to maybe missing or bad inputs to the google_distance() call where NULL is returned in corresponding position in your res list.

Consider wrapping tryCatch to return a one-row data frame of NAs for those problem elements. If all elements emerge as one-row NA's then all runs of google_distance() failed.

lst_elements <- lapply(res, function(x){
   tryCatch(setNames(cbind(distance_elements(x)[[1]][['duration']],
                           distance_elements(x)[[1]][['distance']]
                     ), c("duration_text", "duration_value", 
                          "distance_text", "distance_value")
                     ),
            error = function(e) 
                        data.frame(duration_text=NA, duration_value=NA, 
                                   distance_text=NA, distance_value=NA)
            )      
})

final_df <- do.call(rbind, lst_elements)
Parfait
  • 104,375
  • 17
  • 94
  • 125
  • 1
    similarly it could be where Google couldn't find the locations, and therefore couldn't find the distances. I think you can also check for `res$status == "OK"` – SymbolixAU Mar 09 '19 at 09:32
  • Still trouble shooting. I know that "res" has the expected number of list. I know that at least some of the list were successful and have values. I need to find a way to isolate the "null" out of approximately 1200 list in "res" – Rho Apr 03 '19 at 02:09