首页 > 解决方案 > 试图了解 .External() 是什么?

问题描述

我对 R 相当不熟悉,但希望用它来尝试使用特定的包。我从 R-Forge 安装了以下软件包:

install.packages("ecomodtools", repos = "https://R-Forge.R-project.org")

我收到以下错误:

仅提供源代码的软件包,可能需要编译 C/C++/Fortran:'ecomodtools'</p>

并了解到我需要“RTools”来安装它。所以我得到了 RTools,我能够安装这个包。

我在下面包含了一些可以复制粘贴的示例代码,但是当我运行此代码时,我收到关于 .External() 的错误,这是我想要理解的:

library(ecomodtools)

# 2D Gaussian dispersal function defined according to Clark et al. (1999)
# Used in the Monte Carlo integration
genexp.disp <- function(from, to, params)
{
  r.sq <- (from[1] - to[1])^2 + (from[2] - to[2])^2
  (1.0 / (pi * params[1]^2)) * exp(-(r.sq / params[1]^2))
}

# Monte Carlo integration settings
max.prob <- .Machine$double.eps^0.25
initial.step <- 10000
step.size <- 10000
max.runs <- 1000000
max.rel.var <- .Machine$double.eps^0.25

# Boundary condition
boundary <- "restricting"

# Alpha parameter setting
params <- c(1.0)

# Make a small grid (3 x 3) for testing purposes
x.coords <- seq(0.0, 3.0, 1.0)
y.coords <- seq(0.0, 3.0, 1.0)
test.grid <- MakeGridFromCoords(x.coords, y.coords)

# Calculate the transition matrices for each different approximation method

AA.mc <- LatticeTransitionProbs(
  x1 = test.grid$x1, x2 = test.grid$x2, y1 = test.grid$y1, y2 = test.grid$y2,
  func = genexp.disp, approx.method = "AA", boundary = boundary, max.prob = max.prob,
  initial.step = initial.step, step.size = step.size, max.rel.var = max.rel.var, max.runs = max.runs, params = params)

错误:

.External("MakeGridFromCoords", x = as.double(x), y = as.double(y), 中的错误:
"MakeGridFromCoords" 不适用于包 "ecomodtools" 的 .External()

在源代码中,这个 .External() 函数似乎在所有函数中,我发现它的定义是“将 R 对象传递给已加载到 R 中的已编译 C/C++ 代码的函数”。

所以我希望有人能够对这里发生的事情给出不同的解释。我认为这个问题是我没有源 C/C++ 代码,所以也许 .External() 没有将信息发送给任何特别的东西。但同样,我对 R 很陌生,所以我不清楚发生了什么。

标签: c++r

解决方案


推荐阅读