我需要一个包含时间和地址距离的数据框(是的,它们是有效的),并且当mapdist模式为“驾驶”时一切正常。
当我将模式更改为“transit”时,问题就会出现 - 这里有时会返回状态“ZERO_RESULTS”,这意味着没有公共连接(此时)。 那完全没问题,但我不知道如何更改代码以将 NA 写入我的数据帧而不是抱怨:
中的错误: 无法回收bind_cols()
(尺寸 16)来匹配..1
(尺寸 14)。..2
这是我的代码和数据:
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")
当每条记录得到一个结果时,PubTrans 包含: 从、到、米、公里、英里、秒、分钟、小时、模式
这里有一些 html 格式的示例结果:
好的:
{ “目的地地址”:[ “6430 奥茨塔尔火车站,奥地利” ], “来源地址”:[ “26919 制动器(Unterweser),德国” ], “行”:[ { “元素”:[ { “距离”: { “文本”:“1.082公里”, “值”:1082492 }, “期间”: { "text": "12 斯通登,27 分钟", “值”:44816 }, “状态”:“正常” } ] } ], “状态”:“正常” }
没有结果:
{ “目的地地址”:[ “6430 奥茨塔尔火车站,奥地利” ], “来源地址”:[ “霍施泰特,德国” ], “行”:[ { “元素”:[ { “状态”:“ZERO_RESULTS” } ] } ], “状态”:“正常” }
我不知道如何解决这个问题 - 希望有任何建议!
tryCatch()
函数rlang包中的
tryCatch()
函数是一个通用的错误处理函数。它允许您捕获计算表达式时发生的错误和警告,并以受控的方式处理它们。
有了这个,你可以处理错误,
<error/vctrs_error_incompatible_size> Error in bind_cols(): Can't recycle ..1 (size 16) to match ..2 (size 14).
只返回你想要的。
要在代码中使用
tryCatch()
,您必须首先注意 tryCatch()
函数需要三个参数:
expr
:要计算的表达式。error
:在计算 expr 时发生错误时调用的处理函数。warning
:在计算 expr 时发生警告时调用的处理函数。错误和警告处理函数可以是任何采用单个参数的函数,该参数是错误或警告消息。处理函数可以返回任何值,这将是
tryCatch()
函数的返回值。
如果在计算 expr 时没有发生错误或警告,则
tryCatch()
函数将仅返回计算 expr 的结果。
在您的情况下,您可以使用
mapdist()
函数作为表达式,然后处理 tryCatch()
的第二个参数中的错误。它应该看起来像这样:
result <- tryCatch(mapdist(from = gmap.trans$gmap.address,
+ to = gmap.trans$dest,
+ mode = "transit",
+ output = "simple"), error = function(e) {print(NA)})
这会导致这样的结果:
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
如您所见,我刚刚在
NA
参数中打印了 error
来复制您在本问题中提到的想要的内容,但您可以根据需要进行修改。
我希望这有帮助!