问题描述
来自这本书 here
library(maptools)
s.owin <- as(s.sp,"owin")
class(s.owin)
应该可以工作,但它没有并抛出错误 'spatstat.options' is not an exported object from 'namespace:spatstat'
。 spatstat.options()
存在并且工作正常(对我来说):
spatstat.options()
# $areainter.polygonal
# [1] FALSE
#
# $checksegments
# [1] TRUE
# ...
我使用最新版本的 statspat:packageVersion("spatstat")
给出 2.0.1
。 RStudio 不显示更新过程的任何依赖包:
x <- installed.packages()
x[grepl("spatstat",x[,"Depends"]),"Package"]
spatstat spatstat.core spatstat.geom spatstat.linnet
"spatstat" "spatstat.core" "spatstat.geom" "spatstat.linnet"
p.ppp <- as(p.sp,"ppp") # Create ppp object
抛出同样的错误...
可重现的例子:
library(sp)
Grd <- sp::GridTopology(c(1,1),c(1,c(10,10))
polys <- as(Grd,"Spatialpolygons")
centroids <- sp::coordinates(polys)
x <- centroids[,1]
y <- centroids[,2]
z <- 1.4 + 0.1*x + 0.2*y + 0.002*x*x
spdf <- sp::SpatialpolygonsDataFrame(polys,data = data.frame(x=x,y=y,z=z,row.names=row.names(polys)))
library(raster)
plot(spdf)
library(sf)
spsf <- st_as_sf(spdf)
plot(spsf$geometry)
plot(spsf)
library(maptools)
library(spatstat)
s.owin <- as(spdf,"owin") # calls as.owin.Spatialpolygons(from) but to me it is not clear why and I can't debug here...
# Fehler: 'spatstat.options' is not an exported object from 'namespace:spatstat'
spatstat.options()
解决方法
虽然 RStudio 告诉我,所有软件包都是最新的,但问题仍然存在。 解决方案是对 R 和所有软件包进行全面更新。该过程在 Windows 上:
- 从 Rgui.exe 运行
installr::update()
(在 \R\R-4.0.4\bin\x64 中)。 - 更新 Windows 环境变量
R_LIBS
新的 \R\R-4.0.4\library。 - 更新\R\R-4.0.4\etc 中的Rprofile.site 并确保只有一个
.libPaths()
。 (必须有一行.libPaths("C:/R/R-4.0.4/library")
或添加它。) - 检查 RStudio 中是否有待处理的软件包更新
刚刚遇到这个问题,可以确认更新包是修复。我实际上无法按照您的说明进行操作(我不太精明)但下面的简单代码有效:
首先重新启动 R,以便清除所有预加载的包并可以更新
# list of packages where an update is available
old.packages()
# update all available packages
update.packages()
# update,without prompts for permission/clarification
update.packages(ask = FALSE)