gpt4 book ai didi

R:with()函数内的 Debug模式

转载 作者:行者123 更新时间:2023-12-02 21:33:12 24 4
gpt4 key购买 nike

出于某种原因,当我的函数体位于 with() 表达式内时, Debug模式似乎不允许我进入 with() 部分。为什么会这样,有办法解决这个问题吗?下面是一个愚蠢的(但希望可以重现)演示。

    ff=function(x){
print("Hello")
with(iris,{
y=x;
z=y+mean(Sepal.Width);
return(z);})
}

现在进入 Debug模式并尝试该功能...

    debugonce(ff);debugonce(with);
ff(10)

Debug模式只是跳过 with() 子句,并返回答案 (13.05733)。我如何进入这些内部条款?

最佳答案

这是可行的,只是你期望它做的事情并不是它做的事情。 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.default如果你想看看 with 发生了什么更详细,但我怀疑这会达到你想要的效果。我不知道如何间接做到这一点(即使您可以调试 { ,我认为您不能这样做,但这不会有帮助,因为您将查看 { 的代码,而不是参数到 { ,与 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);})
}

关于R:with()函数内的 Debug模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21921709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com