为什么 as(spdf, "owin") 抛出错误 'spatstat.options' 不是 'namespace:spatstat' 的导出对象

Why does as(spdf, "owin") throw an error 'spatstat.options' is not an exported object from 'namespace:spatstat'

来自本书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,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:

上的进程
  • 运行 installr::update()来自 Rgui.exe(在 \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)