R Studio 中奇怪的调试结果 [英] Strange debug results in R Studio

查看:39
本文介绍了R Studio 中奇怪的调试结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须说我对 RStudio/R 还是很陌生,所以我希望这不是用户错误.但是,在调试时,我在调试模式下得到了一些奇怪的结果.

I must predicate this by saying that I am still quite new to RStudio/R so I hope this is not user error. However when debugging I am getting some strange results whilst in debug mode.

1) 代码在没有明显被调用的情况下跳转到代码的另一部分.

1) The code jumps to another part of the code without apparently being called.

2) 发生这种情况时,荧光笔仅部分突出显示它跳转到的代码,但边距中的箭头指向这条部分突出显示的行

2) When this happens the highlighter only partially highlights the code it jumps to but the arrow in the margin points to this partially highlighted line

3) 然后代码返回到原来跳离的地方

3) The code then returns to place it jumped away from originally

我收到警告调试位置是近似的,因为源代码不可用".这与问题有关吗?

I get the warning "Debug location is approximate because source source code is not available". Is this related to the problem?

您可以通过安装 dlm 包来复制结果:

You can replicate the results by installing the package dlm:

https://cran.r-project.org/web/包/dlm/dlm.pdf

 p<-3
 G=diag(c(0.9963,0.9478,0.7740))
 W=diag(c(0.0026^2,0.0027^2,0.0035^2))
 C0_est=solve(diag(p)-G%*%t(G))*W
 mod2<-dlm(m0=c(0.0501,-0.0251,-0.0116),C0=C0_est,FF=X,V=0.000000001*diag(m),GG=diag(c(0.9963,0.9478,0.7740)),W=diag(c(0.0026^2,0.0027^2,0.0035^2)))
 debugonce(dlmForecast)
 dlmForecast(mod2, nAhead=5, sampleNew=2)

当您逐步使用 F10 时,您会发现代码从第 28、32、54 和 57 行跳转到 L7,从第 47 行跳转到 L4.

When you step through using F10 you will find that the code jumps to L7 from Lines 28,32, 54 and 57 and to L4 from line 47.

Q1.) 为什么要调试的行的部分突出显示是什么意思?

Q1.) Why does this partial highlighting of the line being debugged mean?

Q2.) 是什么导致代码像这样来回跳转而没有明显的函数调用?

Q2.) What causeses the code to jump backward and forwards like this with no apparent function call?

亲切的问候

巴兹

推荐答案

大多数 R 包不包含包源代码的副本(keep.source 选项控制此行为;您可以阅读更多关于它这里).当您从这种包中调试代码时,RStudio 必须猜测代码最初的样子以及当前执行点在该代码中的位置.此过程包括一些启发式方法,并且偶尔会误解执行点.所以你看到的不是实际执行点的跳跃,而是 RStudio 对它在解析后的代码中的位置做出了错误的猜测.

Most R packages don't include copies of the package's source code (the keep.source option controls this behavior; you can read more about it here). When you debug code from this kind of package, RStudio has to guess at both what the code looked like originally and where the current execution point is in that code. This process includes some heuristics and does occasionally misinterpret the execution point. So what you're seeing is not the actual execution point jumping around, but RStudio making the wrong guess about where it is in the deparsed code.

来自您自己的 R 脚本(以及来自带有 keep.source 的包)的代码具有源引用(请参阅 R 期刊文章) 允许 RStudio 精确排列代码和执行点.

Code from your own R scripts (and from packages with keep.source) have source references (see R journal article) which allow RStudio to line up the code and execution point precisely.

我无法在您的示例中重现这一点(它似乎引用了一个未声明的变量 m);如果你能解决这个问题并将其发送给我(rstudio dot com 的乔纳森),我会看看我们是否可以改进启发式方法,使这种情况更好.

I wasn't able to reproduce this with your example (it appears to reference a variable m that it doesn't declare); if you can resolve that and send it to me (jonathan at rstudio dot com) I'll see if we can improve the heuristics so this case works better.

这篇关于R Studio 中奇怪的调试结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆