Reputation: 11
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!
Upvotes: 1
Views: 150
Reputation: 2561
tryCatch()
function in RThe
tryCatch()
function in the rlang package is a general-purpose error handling function. It allows you to catch errors and warnings that occur while evaluating an expression, and to handle them in a controlled way.
With 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,
the
tryCatch()
function takes three arguments:
expr
: The expression to evaluate.error
: A handler function that is called if an error occurs while evaluating expr.warning
: A handler function that is called if a warning occurs while evaluating expr.The error and warning handler functions can be any function that takes a single argument, which is the error or warning message. The handler function can return any value, which will be the return value of the
tryCatch()
function.If no error or warning occurs while evaluating expr, then the
tryCatch()
function will simply return the result of evaluating expr.
In your case, you can use the mapdist()
function as an expression then handle the error in the second argument of the tryCatch()
. It should look like this:
result <- tryCatch(mapdist(from = gmap.trans$gmap.address,
+ to = gmap.trans$dest,
+ mode = "transit",
+ output = "simple"), error = function(e) {print(NA)})
And this will result in this:
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=10249+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=10315+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=10369+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=10439+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=10717+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=10719+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=12489+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=12527+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=12621+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=13187+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=14052+berlin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=15370+petershagen/eggersdorf,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=15806+trebbin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=17192,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=18609+binz,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
i <https://maps.googleapis.com/maps/api/distancematrix/json?origins=19053+schwerin,+germany&destinations=Oetztal+Bahnhof+,+%C3%96sterreich&key=xxx&mode=transit>
[1] NA
As you can see, I just printed NA
in the error
argument 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!
Upvotes: 0