目标::我正在尝试使用get_map
中的ggmaps
函数检索一系列地图。
使用纬度和经度时,我知道以下作品:
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个地址,很可能找不到几个地址。
答案 0 :(得分:1)
由于我都没有示例数据(请始终在您的问题中提供数据),并且
也不知道我要演示的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"
答案 1 :(得分:1)
使用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")}
)
我本人无法对此进行测试,因此希望我将所有括号都关闭了,但是您明白了它的要旨。