I need a dataframe with time and distance of addresses (and yes, they are valid) and everything works when the mapdist mode is 'driving'.
The problem occurs as soon as I change the mode to 'transit' - here it sometimes return the status "ZERO_RESULTS", what just means, there is no public connection (at this time). That would be perfectly fine, but I don't know how I could change the code to write NA's to my dataframe instead of complaining:
<error/vctrs_error_incompatible_size> Error in
bind_cols(): Can't recycle..1(size 16) to match..2(size 14).
Here is my code with data:
ggmap::register_google("your_key", write=TRUE)
gmap.trans <- data.frame(
gmap.address = c("13187 berlin, germany", "14052 berlin, germany", "10315 berlin, germany", "10249 berlin, germany", "18609 binz, germany", "10369 berlin, germany", "15370 petershagen/eggersdorf, germany", "15806 trebbin, germany", "10719 berlin, germany", "17192, germany", "19053 schwerin, germany", "10717 berlin, germany", "10439 berlin, germany", "12489 berlin, germany", "12527 berlin, germany", "12621 berlin, germany"),
dest = c("Oetztal Bahnhof , Österreich"))
PubTrans <- mapdist(from = gmap.trans$gmap.address,
to = gmap.trans$dest,
mode = "transit",
output = "simple")
When every record gets a result PubTrans contains: from, to, m, km, miles, seconds, minutes, hours, mode
Here some example outcomes in html:
OK:
{ "destination_addresses": [ "6430 Ötztal Bahnhof, Österreich" ], "origin_addresses": [ "26919 Brake (Unterweser), Deutschland" ], "rows": [ { "elements": [ { "distance": { "text": "1.082 km", "value": 1082492 }, "duration": { "text": "12 Stunden, 27 Minuten", "value": 44816 }, "status": "OK" } ] } ], "status": "OK" }
NO RESULT:
{ "destination_addresses": [ "6430 Ötztal Bahnhof, Österreich" ], "origin_addresses": [ "Horstedt, Deutschland" ], "rows": [ { "elements": [ { "status": "ZERO_RESULTS" } ] } ], "status": "OK" }
I have no idea how to solve this problem - would appreciate any suggesstion!
You can use the
tryCatch()function in RWith this, you can handle the error,
<error/vctrs_error_incompatible_size> Error in bind_cols(): Can't recycle ..1 (size 16) to match ..2 (size 14).to only return what you want.To use
tryCatch()in your code, you must take note first that,In your case, you can use the
mapdist()function as an expression then handle the error in the second argument of thetryCatch(). It should look like this:And this will result in this:
As you can see, I just printed
NAin theerrorargument to replicate what you wanted as mentioned in this questions, but it's up to you to modify it however you want.I hope this helps!