Christoph
Christoph

Reputation: 7063

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

From this book here

library(maptools)
s.owin <- as(s.sp, "owin")
class(s.owin)

should work, but it does not and throws the error 'spatstat.options' is not an exported object from 'namespace:spatstat'. spatstat.options() exists and works fine (to me):

spatstat.options()
# $areainter.polygonal
# [1] FALSE
#
# $checksegments
# [1] TRUE
# ...

I use the latest version of statspat: packageVersion("spatstat") gives 2.0.1. RStudio does not show any dependent package for the update process:

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 throws the same error...


Reproducible example:

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()

Upvotes: 1

Views: 1587

Answers (2)

Megan Halbrook
Megan Halbrook

Reputation: 85

just had this problem and can confirm that updating packages was the fix. I actually couldn't follow your instructions (I'm not too savvy) but the below simple code worked:

first- restart R so any preloaded packages are cleared and can be updated

    # 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)

Upvotes: 1

Christoph
Christoph

Reputation: 7063

Although RStudio told me, all packages were up to date, the problem continued to exist. The solution was a full update of R and all packages. The process on Windows:

  • Run installr::update()from Rgui.exe (in \R\R-4.0.4\bin\x64).
  • Update Windows environment variable R_LIBSthe the new \R\R-4.0.4\library.
  • update Rprofile.site in \R\R-4.0.4\etc and make sure there is only one .libPaths(). (There has to be a line .libPaths("C:/R/R-4.0.4/library") or just add it.)
  • Check if there are pending package updates in RStudio

Upvotes: 3

Related Questions