R package vegan stepacross function error

38 Views Asked by At

I am trying to use the stepacross function in the vegan package of R.

When I do, it throws an error and fails to run the code

Error in stepacross(distance.dat, path = "extended") : object 'C_stepacross' not found

Anybody know the cause of this or what to do to fix it?

I'm using R (64-bit) 4.0.2 and vegan 2.5-6 (old version use is intentional).

It worked a few weeks ago, and I have made no changes since.

The C_stepacross object in question shows up in the stepacross() function code:

getAnywhere(stepacross)

function (dis, path = "shortest", toolong = 1, trace = TRUE, 
    ...) 
{
    path <- match.arg(path, c("shortest", "extended"))
    if (!inherits(dis, "dist")) 
        dis <- as.dist(dis)
    oldatt <- attributes(dis)
    n <- attr(dis, "Size")
    if (path == "shortest") 
        dis <- .C(dykstrapath, dist = as.double(dis), n = as.integer(n), 
            as.double(toolong), as.integer(trace), out = double(length(dis)), 
            NAOK = TRUE)$out
    else dis <- .C(C_stepacross, dis = as.double(dis), as.integer(n), 
        as.double(toolong), as.integer(trace), NAOK = TRUE)$dis
    attributes(dis) <- oldatt
    attr(dis, "method") <- paste(attr(dis, "method"), 
        path)
    dis
}
1

There are 1 best solutions below

1
Jari Oksanen On

You don't provide a reproducible example, but I can reproduce this problem if I don't use vegan::stepacross, but a different copy of the function. Check your workspace – it probably has a copy of this function. The C function is registred for use in vegan functions, but not for functions in other namespaces. This example will reproduce your problem:

library(vegan)
data(dune)
d <- vegdist(dune)
stepacross <- vegan::stepacross
environment(stepacross) <- environment() ## set to working environment
dd <- stepacross(d, "ext") 
## Error in stepacross(d, "ext") : object 'C_stepacross' not found
dd <- vegan::stepacross(d, "ext") ### this will be OK
rm(stepacross) ## removes the local copy
dd <- stepacross(d, "ext") ## this will be OK: vegan copy was untouched

If getAnyewhere finds first a vegan version of stepacross, the last line of its output will be

<environment: namespace:vegan>

In your example this line was missing suggesting that your copy of stepacross was not in namespace:vegan. Moreover, getAnywhere should give package:vegan as the first place where this function was found.