我对 R 很陌生,我正在尝试创建一个具有一些地理输出的函数。我正在尝试包含一些 if/else 语句来为某些参数(如地图标题)设置默认值,但反复遇到相同的问题。其他人也有同样的问题,但他们的询问对我没有帮助。
下面是我的代码的简化版本,以及我遇到的错误。
my_function <- function(x, y, map.title, a, ... ){
neighbours <- spdep::poly2nb(x, queen=T, snap=T)
print("neighbours defined")
local <- spdep::localmoran(y, listw=nb2listw(neighbours, style="W"))
moran_map <- x
moran_map@data <- cbind(x@data, local)
if(map.title = NULL) {
seg_map <- tmap::tm_shape(moran_map) +
tm_fill(col = "Ii",
style = "quantile",
title = "Local Moran's I Statistic") +
tm_layout(title = "Good Maps Have Titles")
return(map)
} else {
seg_map <- tmap::tm_shape(moran_map) +
tm_fill(col = 'Ii',
style = 'quantile',
title = "Local Moran's I Statistic",
palette = a) +
tm_layout(title = map.title)
return(map)
}
}
我不断收到以下错误。
>Error: no function to return from, jumping to top level
> }
Error: unexpected '}' in " }"
> }
Error: unexpected '}' in " }"
有谁知道我做错了什么,我该如何解决这个问题?
非常感谢!
在你得到这些错误之前,你有一个更早的错误:
+ if(map.title = NULL) {
Error: unexpected '=' in:
" moran_map@data <- cbind(x@data, local)
if(map.title ="
不允许=
在if
测试中使用单个。通常你==
用来测试一个值的相等性,但NULL
很特殊,所以在这里你会想要is.null(map.title)
(正如 Parfait 所说的)。
当 R 在该语句中发现错误时,它停止函数定义并重新开始;后来的陈述没有意义,所以你得到了这些错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句