JUAN MIGUEL REQUENA MULLOR
unread,Apr 11, 2025, 3:30:29 AMApr 11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to R-inla discussion group
Hi all,
I have found a workaround for the error, but I would like to fix it more elegantly.
Here is the code, the input data is attached.
library("INLA")
library("raster")
library("INLAspacetime")
set.seed(201805)
# load required spatial files for building the mesh
greenhouses <- shapefile("/home/jrequena/sampling_simulation/domain.shp")
points <- spsample(greenhouses, n = 40, type = "random")# extract a random sample of points outside greenhouses
range <- 1000 # in meters
bound.outer = 200
max.edge <- (1/10) * range
# Build the mesh; lengths in meters
mesh = inla.mesh.2d(boundary = greenhouses,
loc=cbind(points$x, points$y),
max.edge = c(0.5,4)*max.edge,
cutoff = 2,
crs = CRS("+proj=utm +zone=30 +datum=WGS84 +units=m +no_defs"),
offset = c(max.edge, bound.outer))
# select the triangles of the mesh that are inside greenhouses
# water.tri = fm_contains(greenhouses, y = mesh, type = "centroid", ignore.CRS = TRUE)
water.tri = inla.over_sp_mesh(greenhouses, y = mesh, type = "centroid", ignore.CRS = TRUE)
num.tri = length(mesh$graph$tv[, 1])
barrier.tri = setdiff(1:num.tri, water.tri[[1]])
poly.barrier = inla.barrier.polygon(mesh,
barrier.triangles = barrier.tri)
crs(poly.barrier) <- CRS("+proj=utm +zone=30 +datum=WGS84 +units=m +no_defs")
# define the precision matrix for the Barrier model considering unit marginal variance
prec <- 1
barrier.model <- inla.barrier.pcmatern(mesh, prior.range = c(range,0.5), prior.sigma = c(prec,0.5),
barrier.triangles = barrier.tri, range.fraction = 0.5, enable.INLAspacetime = FALSE)
Error in as.character(package) :
cannot coerce type 'closure' to vector of type 'character'
--------------------------------------------------------------------------------------------------------------
I have looked deeper at the error as follows:
The error arises when running "requireNamespace(methods)" inside the function inla.barrier.pcmatern()
traceback()
6: requireNamespace(methods)
5: graph(NULL)
4: do.call(match.arg(cmd), args = list(theta))
3: model(cmd = "graph", theta = NULL)
2: inla.rgeneric.define(model = barrier.rgeneric.model, optimize = TRUE,
obj = obj)
1: inla.barrier.pcmatern(mesh, prior.range = c(range, 0.5), prior.sigma = c(prec,
0.5), barrier.triangles = barrier.tri, range.fraction = 0.5,
enable.INLAspacetime = FALSE)
> requireNamespace(methods)
Error in as.character(package) :
cannot coerce type 'closure' to vector of type 'character'
> print(body(requireNamespace))
{
package <- as.character(package)[[1L]]
ns <- .Internal(getRegisteredNamespace(package))
res <- TRUE
if (is.null(ns)) {
if (!quietly)
packageStartupMessage(gettextf("Loading required namespace: %s",
package), domain = NA)
value <- tryCatch(loadNamespace(package, ...), error = function(e) e)
if (inherits(value, "error")) {
if (!quietly) {
msg <- conditionMessage(value)
cat("Failed with error: ", sQuote(msg), "\n",
file = stderr(), sep = "")
.Internal(printDeferredWarnings())
}
res <- FALSE
}
}
invisible(res)
}
So, I have replaced "requireNamespace(methods)" with require(methods)" inside the function inla.barrier.pcmatern() and have created it again. This worked but I guess it is not the best way to fix it.
Any help is really welcome!
Thank you in advance.
Juanmi