如果找不到地址继续循环
Continue looping if address not found
AIM: 我正在尝试使用 ggmaps
中的 get_map
函数检索一系列地图。
当我使用经纬度时,我知道以下工作:
houses_maps <- lapply(latlon,
function(x)
get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google"))
问题: 当我使用地址而不是纬度和经度时,它没有完成循环。这可能是因为它没有找到地址之一,例如 "tomet, 6-10, 25720 Bellver de Cerdanya, Lleida, Spain"
我收到这个错误:
Error in data.frame(ll.lat = ll[1], ll.lon = ll[2], ur.lat = ur[1], ur.lon = ur[2]) :
arguments imply differing number of rows: 0, 1
In addition: Warning message:
geocode failed with status ZERO_RESULTS, location = "tomet, 6-10, 25720 Bellver de Cerdanya, Lleida, Spain"
Called from: data.frame(ll.lat = ll[1], ll.lon = ll[2], ur.lat = ur[1], ur.lon = ur[2])
问题: 我怎样才能让它忽略它找不到的地址并保留它们 NA 并继续搜索其余地址而不是停止。我有 2,000 个地址,它可能找不到几个。
因为我既没有示例数据(请始终在您的问题中提供数据)并且
也不了解我正在演示的 get_map
函数的许多细节
这里只是基本的想法:
# simplified example data
latlon = c("address 1", "address 2", "address 3")
# mock the function
get_map <- function(location, ...) {
if (location == "address 2") stop(paste("geocode failed with status ZERO_RESULTS, location =", location))
return(location)
}
houses_maps <- lapply(latlon,
function(x)
tryCatch(get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google"),
error = function(e) {
print(e)
return(NA)
}))
# <simpleError in get_map(location = x, zoom = 20, maptype = "satellite",
# source = "google"): geocode failed with status ZERO_RESULTS,
# location = address 2>
houses_maps
# [[1]]
# [1] "address 1"
#
# [[2]]
# [1] NA
#
# [[3]]
# [1] "address 3"
事先使用try命令实际测试功能。在您的示例中,它应该是:
houses_maps <- lapply(latlon,
function(x)
res <- try(get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google"))
if(inherits(res, "try-error")) next
else{
get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google")}
)
我自己无法对此进行测试,所以希望我关闭了所有括号,但你已经明白了它的要点。
AIM: 我正在尝试使用 ggmaps
中的 get_map
函数检索一系列地图。
当我使用经纬度时,我知道以下工作:
houses_maps <- lapply(latlon,
function(x)
get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google"))
问题: 当我使用地址而不是纬度和经度时,它没有完成循环。这可能是因为它没有找到地址之一,例如 "tomet, 6-10, 25720 Bellver de Cerdanya, Lleida, Spain"
我收到这个错误:
Error in data.frame(ll.lat = ll[1], ll.lon = ll[2], ur.lat = ur[1], ur.lon = ur[2]) :
arguments imply differing number of rows: 0, 1
In addition: Warning message:
geocode failed with status ZERO_RESULTS, location = "tomet, 6-10, 25720 Bellver de Cerdanya, Lleida, Spain"
Called from: data.frame(ll.lat = ll[1], ll.lon = ll[2], ur.lat = ur[1], ur.lon = ur[2])
问题: 我怎样才能让它忽略它找不到的地址并保留它们 NA 并继续搜索其余地址而不是停止。我有 2,000 个地址,它可能找不到几个。
因为我既没有示例数据(请始终在您的问题中提供数据)并且
也不了解我正在演示的 get_map
函数的许多细节
这里只是基本的想法:
# simplified example data
latlon = c("address 1", "address 2", "address 3")
# mock the function
get_map <- function(location, ...) {
if (location == "address 2") stop(paste("geocode failed with status ZERO_RESULTS, location =", location))
return(location)
}
houses_maps <- lapply(latlon,
function(x)
tryCatch(get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google"),
error = function(e) {
print(e)
return(NA)
}))
# <simpleError in get_map(location = x, zoom = 20, maptype = "satellite",
# source = "google"): geocode failed with status ZERO_RESULTS,
# location = address 2>
houses_maps
# [[1]]
# [1] "address 1"
#
# [[2]]
# [1] NA
#
# [[3]]
# [1] "address 3"
事先使用try命令实际测试功能。在您的示例中,它应该是:
houses_maps <- lapply(latlon,
function(x)
res <- try(get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google"))
if(inherits(res, "try-error")) next
else{
get_map(location = x,
zoom = 20,
maptype = "satellite",
source = "google")}
)
我自己无法对此进行测试,所以希望我关闭了所有括号,但你已经明白了它的要点。