R trace在函数内部无法正常工作?

时间:2015-10-08 16:46:16

标签: r trace

trace似乎无法在预编译函数中正常工作

例如,在此代码段中

xx <- 2:7
nu <- seq(-10, 9, length.out = 2001)
op <- par(lab = c(16, 5, 7))
matplot(nu, t(outer(xx, nu, besselI)), type = "l", ylim = c(-50, 200),
        main = expression(paste("Bessel ", I[nu](x), " for fixed ", x,
                                ",  as ", f(nu))),
        xlab = expression(nu))

trace的简单xy.coords正常工作

trace(xy.coords)
matplot(nu, t(outer(xx, nu, besselI)), type = "l", ylim = c(-50, 200),
        main = expression(paste("Bessel ", I[nu](x), " for fixed ", x,
                                ",  as ", f(nu))),
        xlab = expression(nu))
# trace: xy.coords
# trace: xy.coords
# trace: xy.coords
# trace: xy.coords
# trace: xy.coords
# trace: xy.coords
# trace: xy.coords

但是使用函数进行跟踪似乎不起作用

trace(xy.coords, tracer = quote(cat("test\n")))
# Tracing function "xy.coords" in package "grDevices"
# [1] "xy.coords"
matplot(nu, t(outer(xx, nu, besselI)), type = "l", ylim = c(-50, 200),
        main = expression(paste("Bessel ", I[nu](x), " for fixed ", x,
                                ",  as ", f(nu))),
        xlab = expression(nu))

直接通话正常

xy.coords(1:3, 1:2, recycle = TRUE)
# Tracing xy.coords(1:3, 1:2, recycle = TRUE) on entry 
# test
# $x
# [1] 1 2 3
# 
# $y
# [1] 1 2 1
# 
# $xlab
# NULL
# 
# $ylab
# NULL

发生了什么,我需要改变什么?

更新我禁用了grDevices和其他base个软件包的编译,但trace仍然无法正常运行。调试matplot时,xy.coords似乎没有跟踪。

更新2 这似乎与Override a function that is imported in a namespace有关,但在尝试了所有建议后,在命名空间中为被跟踪对象分配时,仍会调用旧对象。

1 个答案:

答案 0 :(得分:1)

对图形的导入(matplot所属的)进行操作似乎可以解决问题:

xx <- 2:7
nu <- seq(-10, 9, length.out = 2001)
op <- par(lab = c(16, 5, 7))

trace(xy.coords, tracer = quote(cat("test\n")))

# get the imports env of graphics
nsi <- parent.env(getNamespace('graphics'))

unlockBinding("xy.coords", nsi)
assign('xy.coords', xy.coords, nsi)

matplot(nu, t(outer(xx, nu, besselI)), type = "l", ylim = c(-50, 200),
        main = expression(paste("Bessel ", I[nu](x), " for fixed ", x,
                                ",  as ", f(nu))),
        xlab = expression(nu))


Tracing xy.coords(x, y, xlabel, ylabel, log = log) on entry 
test
Tracing xy.coords(x, y, xlabel, ylabel, log) on entry 
test
Tracing xy.coords(x, y) on entry 
test
Tracing xy.coords(x, y) on entry 
test
Tracing xy.coords(x, y) on entry 
test
Tracing xy.coords(x, y) on entry 
test
Tracing xy.coords(x, y) on entry 
test