值不被复制到下一个(本地)环境? [英] values not being copied to the next (local) environment?
问题描述
考虑位于 calcDistance
内的 browser()
的输出:
Called from: calcDistance(object = rst, xy = xy[[i]][j, ], effect.distance = effect.distance)
Browse[1]> ls.str()
effect.distance : num 236
object : Formal class 'RasterLayer' [package "raster"] with 12 slots
xy : Named num [1:2] -101.8 35.5
Browse[1]>
debugging in: xyValues(object = object, xy = xy, buffer = effect.distance)
debug: standardGeneric("xyValues")
Browse[2]> ls.str()
object : Formal class 'RasterLayer' [package "raster"] with 12 slots
xy : Named num [1:2] -101.8 35.5
函数如下:simulationRun> createDistRaster> calcDistance> raster :: xyValues。在上面的输出中,你只能看到最后两个。 xyValues
来自栅格包。
Functions are as follows: simulationRun > createDistRaster > calcDistance > raster::xyValues. In the above output, you only see the last two. xyValues
is from raster package.
第一段代码显示有三个对象: effect.distance
, object
, xy
。
在第二段中,我们通过调用debug(xyValues)来进入xyValues。
在第三段中我们可以看到缺少 effect.distance
。
First paragraph of code shows that three objects are present: effect.distance
, object
, xy
.
In second paragraph, we descend into xyValues by calling debug(xyValues).
In third paragraph we can see that effect.distance
is missing.
我的问题是:Even尽管 object
和 xy
似乎被复制到 xyValues
环境就好了, effect.distance
不是。
My question is: Even though object
and xy
seem to be copied to the xyValues
environment just fine, effect.distance
is not. How could this be explained?
我的sessionInfo()
My sessionInfo()
R version 2.11.1 (2010-05-31)
i386-pc-mingw32
locale:
[1] LC_COLLATE=Slovenian_Slovenia.1250 LC_CTYPE=Slovenian_Slovenia.1250
[3] LC_MONETARY=Slovenian_Slovenia.1250 LC_NUMERIC=C
[5] LC_TIME=Slovenian_Slovenia.1250
attached base packages:
[1] splines stats graphics grDevices utils datasets methods
[8] base
other attached packages:
[1] raster_1.3-11 foreach_1.3.0 codetools_0.2-2 iterators_1.0.3
[5] Hmisc_3.8-2 survival_2.35-8 spam_0.22-0 splancs_2.01-27
[9] sp_0.9-66 spatstat_1.20-2 deldir_0.0-12 mgcv_1.6-2
loaded via a namespace (and not attached):
[1] cluster_1.12.3 grid_2.11.1 lattice_0.18-8 Matrix_0.999375-39
[5] nlme_3.1-96 tools_2.11.1
推荐答案
UPDATE:
在R邮件列表中也讨论了这个问题,结果在特定情况下解决传递参数时存在缺陷/不一致。这被报告给R.讨论可以在以下网址找到:
Nabble
非常有趣的问题。当您检查
Quite an interesting problem. When you check
showMethods("xyValues",incl=T)
有两个重要的代码块。具有xy的签名向量和xy作为矩阵的一个。由于你的对象是一个RasterLayer对象,你需要确保origin.point是一个矩阵。如果我们看代码,这实际上是违反直觉的。
There are two important chunks of code. The one with signature vector for xy, and one for xy as a matrix. As your object is a "RasterLayer" object, you need to make sure origin.point is a matrix. This is pretty counterintuitive actually if we look at the code
object="Raster", xy="vector"
function (object, xy, ...)
{
if (length(xy) == 2) {
callGeneric(object, matrix(xy, ncol = 2), ...)
}
else {
stop("xy coordinates should be a two-column matrix or data.frame, or a vector of two numbers.")
}
}
所以这实际上只会将xy参数转换为矩阵,并将所有其他参数传递给下一个通用。下一个必须是这个:
So this actually only transforms the xy argument to a matrix, and passes all other arguments to the next generic. The next one has to be this one then :
object="RasterLayer", xy="matrix"
function (object, xy, ...)
{
.local <- function (object, xy, method = "simple", buffer = NULL,
fun = NULL, na.rm = TRUE)
{
if (dim(xy)[2] != 2) {
stop("xy has wrong dimensions; it should have 2 columns")
}
if (!is.null(buffer)) {
return(.xyvBuf(object, xy, buffer, fun, na.rm = na.rm))
}
if (method == "bilinear") {
return(.bilinearValue(object, xy))
}
else if (method == "simple") {
cells <- cellFromXY(object, xy)
return(.readCells(object, cells))
}
else {
stop("invalid method argument. Should be simple or bilinear.")
}
}
.local(object, xy, ...)
}
这一个采用参数缓冲区。为什么参数值无法在分析树中找到,我不知道,但是可以尝试通过给出矩阵作为输入而不是向量来避免方法级联。
This one takes the argument "buffer". Why the value for the argument can't be found in the parse tree, I have no clue, but you could try to avoid the method cascade by giving a matrix as input instead of a vector.
这篇关于值不被复制到下一个(本地)环境?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!