问题描述
问题:
如何确定正在调用哪个版本的函数?例如,如果我在data.frame上使用 unique
,则假定我正在使用 unique.data.frame
。但是,尽管有 raster :: unique
,但没有 unique.raster
函数。但是,如果使用 trace(unique)
这样的信息,我只会得到正在使用 unique
函数的信息。
How can I find out which version of a function is being called? For example, if I use unique
on a data.frame, I presume that I am using unique.data.frame
. However, there is no unique.raster
function, although there is a raster::unique
. But if I use, e.g., trace(unique)
, I only get information that the unique
function is being used.
例如,我想确认当我呼叫 unique(data.frame(1))
,正在调用 unique.data.frame
。
I'd like to confirm, for example, that when I call unique(data.frame(1))
, unique.data.frame
is being called.
示例:
我很难弄清为什么 unique(raster_object)
在命令行而不是在函数内部起作用的原因。显然, unique.default
在函数内部被调用,所以我想明确指出哪个ʻunique。&
I am having trouble figuring out why unique(raster_object)
works at the command line but not inside a function. Apparently, unique.default
is being called inside the function, so I would like to explicitly state which `unique.&
例如,这有效:
library(raster)
a <- rasterFromXYZ(data.frame(a=1:2,b=2:3,c=3:4))
unique(a)
但是当我将其放入函数中时:
But when I put this in a function:
myfun <- function(){
a <- rasterFromXYZ(data.frame(a=1:2,b=2:3,c=3:4))
b <- crop(a, extent(c(1,2,3,4)))
unique(a)
}
即使软件包使用 raster
作为依赖项,在构建软件包并将其加载到新的R会话中之后,出现错误:
even if the package uses raster
as a dependency, after I build the package and load it in a new R session, I get the error:
> myfun()
Error in unique.default(a) : unique() applies only to vectors
即使 sessionInfo()
显示光栅包已加载。
Even though sessionInfo()
shows that the raster package is loaded.
并且如果我使用 debug(unique)
,似乎并没有告诉我正在调用哪个函数:
and if I use debug(unique)
, it doesn't seem to be telling me which function is being called:
Browse[6]> unique(a)
Error in unique.default(a) : unique() applies only to vectors
Browse[6]> raster::unique(a)
debugging in: raster::unique(a)
debug: standardGeneric("unique")
推荐答案
myfun <- function(){
a <- rasterFromXYZ(data.frame(a=1:2,b=2:3,c=3:4))
b <- crop(a, extent(c(1,2,3,4)))
raster::unique(a)
}
myfun()
#[1] 3 4
如果在S3系统中调度了 unique.raster
,则可以使用 trace(unique)
,但是由于类 rasterLayer的 unique
方法是S4函数,因此无法使用:
If there were a unique.raster
dispatched in the S3 system, you could have used trace(unique)
, but since the unique
method for class "rasterLayer" is an S4 function, that won't work:
> showMethods("unique")
Function: unique (package base)
x="ANY", incomparables="ANY"
x="character", incomparables="missing"
(inherited from: x="ANY", incomparables="ANY")
x="numeric", incomparables="missing"
(inherited from: x="ANY", incomparables="ANY")
x="RasterLayer", incomparables="missing"
x="RasterStackBrick", incomparables="missing"
使用其打包位置:
> trace("unique", browser, where=raster)
Tracing function "unique" as seen from package "raster"
[1] "unique"
> myfun()
Tracing unique(xyz[, 1]) on entry
Called from: eval(expr, envir, enclos)
Browse[1]>
Browse[1]> c
[1] 3 4
> untrace()
Error in methods::.TraceWithMethods(where = <environment>, untrace = TRUE) :
argument "what" is missing, with no default
> untrace("unique", where=raster)
Untracing function "unique" as seen from package "raster"
这篇关于如何找出正在使用哪个(版本)功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!