值没有被复制到下一个(本地)环境?

发布于 2024-09-15 23:14:18 字数 1816 浏览 1 评论 0原文

考虑位于 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 >创建DistRaster >计算距离>栅格::xy值。在上面的输出中,您只能看到最后两个。 xyValues 来自栅格包。

第一段代码显示存在三个对象:effect.distanceobjectxy。 在第二段中,我们通过调用 debug(xyValues) 深入到 xyValues。 在第三段中,我们可以看到缺少 effect.distance

我的问题是:尽管 objectxy 似乎复制到 xyValues 环境就好,effect.distance > 不是。这怎么解释呢?

我的会话信息()

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 

Consider this output from browser() that is located inside calcDistance:

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

Functions are as follows: simulationRun > createDistRaster > calcDistance > raster::xyValues. In the above output, you only see the last two. xyValues is from raster package.

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.

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?

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能怎样 2024-09-22 23:14:18

更新:
R 邮件列表上也讨论了这个问题,结果证明这是在特定情况下解决传递参数时的错误/不一致。这将报告给 R。讨论可以在以下位置找到:

Nabble


相当有趣的问题。当你检查

showMethods("xyValues",incl=T)

There 有两个重要的代码块时。一种具有 xy 的签名向量,另一种将 xy 作为矩阵。由于您的对象是“RasterLayer”对象,因此您需要确保 origin.point 是矩阵。如果我们查看代码,这实际上是非常违反直觉的

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 参数转换为矩阵,并将所有其他参数传递给下一个泛型。那么下一个必须是这个:

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, ...)
}

这个采用参数“buffer”。为什么在解析树中找不到参数的值,我不知道,但是您可以尝试通过提供矩阵而不是向量作为输入来避免方法级联。

UPDATE :
This problem is also discussed on the R mailing list, and it turned out to be a bug/inconsistency in the resolving of passed arguments in specific cases. This is reported to R. The discussion can be found at :

Nabble


Quite an interesting problem. When you check

showMethods("xyValues",incl=T)

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.")
    }
}

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.

大海や 2024-09-22 23:14:18

buffer 参数通过 ... 参数传递。在调试模式下输入 str(list(...))

buffer argument is passed through ... argument. Type str(list(...)) under debug mode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文