这是有效的,它只是你期望它做的不是它所做的。 debug
将查看with
代码,而不是在您作为参数传递的代码中。仔细一看:
> ff(10)
debugging in: ff(10)
debug at #1: {
print("Hello")
with(iris, {
y = x
z = y + mean(Sepal.Width)
return(z)
})
}
Browse[2]> n
debug at #2: print("Hello")
Browse[2]> n
[1] "Hello"
debug at #3: with(iris, {
y = x
z = y + mean(Sepal.Width)
return(z)
})
Browse[2]> n
现在看看这里发生了什么,我们正在调试中with
:
debugging in: with(iris, {
y = x
z = y + mean(Sepal.Width)
return(z)
})
这是关键:
debug: UseMethod("with")
Browse[3]> n
[1] 13.05733
发生了什么?看看with
代码:
> with
function (data, expr, ...)
UseMethod("with")
<bytecode: 0x00000000092f0e50>
<environment: namespace:base>
所以你可以看到,我们并调试一行在with
。如果您想更详细地了解with
中正在发生的事情,也可以调试with.default
,但我怀疑它会做你想做的事情。我不知道如何间接地做到这一点(即使你可以调试{
,我认为你不能这样做,这样做无济于事,因为你会看到{
的代码,而不是{
的参数,与with
),但作为一个黑客可以使用browse()
:
ff=function(x){
print("Hello")
with(iris,{
browser() # <<<--- this will allow you to browse inside the expression
y=x;
z=y+mean(Sepal.Width);
return(z);})
}
据调试'with',它只是你唯一指定的调试,做的方法分派的部分。尝试调试'with.default'。 – joran