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

标签 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))

简单 tracexy.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有关,但是在尝试了所有建议并在命名空间中分配跟踪对象后,旧的仍然被调用。

最佳答案

对图形的导入(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

关于R 跟踪在函数内部无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33021574/

相关文章:

R data.table 替换另一个 data.table 中的值索引

assembly - MIPS 汇编中的 .word 指令

javascript - Highcharter 弃用函数的输出与建议的不同

prolog - 从跟踪中隐藏模块/包

linux - 跟踪 Linux 操作系统上运行的应用程序的外设交互

runtime - 如何跟踪尖峰中的动态指令(在 RISC-V 上)

c# - ASP.NET Web API 日志记录和跟踪

r - 从字符串数组生成累积的字符串数组

R 为每一行生成随机数字

r - 将 boost 多精度与 RcppEigen 结合使用