Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

"Error in as.character(package)" when running inla.barrier.pcmatern().

20 views
Skip to first unread message

JUAN MIGUEL REQUENA MULLOR

unread,
Apr 11, 2025, 3:30:29 AMApr 11
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
domain.zip

Finn Lindgren

unread,
Apr 11, 2025, 3:36:12 AMApr 11
to JUAN MIGUEL REQUENA MULLOR, R-inla discussion group
Did you try requireNamespace(“methods”) ? Might still not solve the issue completely, but if it doesn’t it should at least trigger some actual error in the code.
Finn

On 11 Apr 2025, at 08:30, 'JUAN MIGUEL REQUENA MULLOR' via R-inla discussion group <r-inla-disc...@googlegroups.com> wrote:

Hi all,
--
You received this message because you are subscribed to the Google Groups "R-inla discussion group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to r-inla-discussion...@googlegroups.com.
To view this discussion, visit https://groups.google.com/d/msgid/r-inla-discussion-group/cbfff09f-17f6-4716-8eaa-9324bf42e0a2n%40googlegroups.com.
<domain.zip>

Finn Lindgren

unread,
Apr 11, 2025, 3:39:20 AMApr 11
to JUAN MIGUEL REQUENA MULLOR, R-inla discussion group
But INLA already has methods in Imports, so I think this line shouldn’t need to be there at all.
Finn

On 11 Apr 2025, at 08:36, Finn Lindgren <finn.l...@gmail.com> wrote:

Did you try requireNamespace(“methods”) ? Might still not solve the issue completely, but if it doesn’t it should at least trigger some actual error in the code.

Finn Lindgren

unread,
Apr 11, 2025, 5:28:22 AMApr 11
to JUAN MIGUEL REQUENA MULLOR, R-inla discussion group
Confirmed; requireNamespace() needs a string with the package name:

> requireNamespace(methods)
Error in as.character(package) : 
  cannot coerce type 'closure' to vector of type 'character'
> requireNamespace("methods")
I've made the correction in the package code. (Still not convinced the call is needed at all though; unfortunately there's no automated package test). Finn
--
Finn Lindgren
email: finn.l...@gmail.com
Reply all
Reply to author
Forward
0 new messages