點擊行號的左側,即可設置斷點(或者按下Shift+F9),如果沒有出現,反而出現下圖的警告:

那么只是因為我的壞習慣——寫一段腳本測試的時候都是新建,但不save到本地,不喜歡保存,寫的差不多了才開始取名字保存....
寫一個for循環測試下:
test <- 0
for(i in 1:9){
j <- i+2
test[i+1] <- test[i]+3
k <- i
}

將environment窗口下,選擇grid(如果選來是list的話,換到grid方便查看變量的值)

第一步
點擊腳本窗口的source
(注意,不是run/ctrl+enter,如果這樣運行斷點是不會發揮作用的)

綠色箭頭指向的是當前的停留在的行,即#4代表的第四行代碼
在控制台輸入n或者點擊next,即可運行下一句。(其實我們可以看到,這個斷點和我們直接在那個地方加一句browse()代碼的效果是一樣的)
第二步

所以,我們注意到,點擊下一句的時候,綠色箭頭指向了下一句,上一句才被執行。
也就是說,當前顯示#5,其實上是#5沒有執行,只要#5之前的執行了。
第三步

第四步:

其實是從第二次開始:
{
.doTrace(browser())
test[i + 1] <- test[i] + 3
}
把他們組合在一起了,然后第五步和第六步就是分別進入這兩步。
第五步:

第六步:

第七步(回歸到一般的代碼順序邏輯):

第八步

按下stop或者輸入大寫的Q,推出debug模式。
如果我們在第一步之后就按下continue按鈕

那么相當於我們輸入一個c和n
continue的作用是執行,直至遇到下一個斷點的位置。
所以呢,我們真正調試的時候一般還是用continue較多吧。
上面為止的是Editor breakpoints
Editor breakpoints work by injecting some tracing code into the R function object.
(所以之前看到了.doTrace)
-------------------------------------------------------------------------------------------
接下來是

也就是在函數里,那么是這種空心紅點,且伴隨有警告,

(其實反正就算是之前的
Editor breakpoints也是需要source才生效,我覺得這個警告有點多余啊
)
如果我們修改了內容,會出現如下的警告。

ctrl+S保存即可,那么也就是說,我們每次修改,都應該重新source才可進行調試的,這很正常啊。

當我們調試結束后,會發現變成如下情況:

此后,如果我們再通過testDebug()語句調用函數,那么函數內部的這個斷點就開始起作用啦~
而且此后,我們再在函數內添加斷點,就都是
Editor breakpoints
(The circle outline indicates that RStudio
is aware of the breakpoint, but that it hasn’t been injected. In most cases, you can source() the file to resolve the problem.但是呢,並不如他所說,其實通過source並未能直接解決問題,而是更顯麻煩的一點點運行)
-------------------------------------------------------------------------------------------
browser()
——直接run/或者ctrl+enter即可,無需source
browser()可以看作最低水平的調試工具,它是你代碼的一部分,不依賴於工具和環境的。
browser()雖然原始,但是在一些按條件調試的情況下還是蠻有用的
for (i in 1:10) {
if (i == 5)
browser()
}
這里的意思是,從i等於5開始調試(不是僅有i=5的時候調試一次哦~),直至一個循環或者函數結束,調試結束。(參考的文章中的start_work()壓根沒有這個函數呃
)
-------------------------------------------------------------------------------------------
debugonce()
——直接run/或者ctrl+enter即可,無需source
比如:
library(fGarch)
debugonce(fGarch::garchFit)
# debugonce(fGarch::.garchArgsParser)
# debugonce(fGarch::.garchFit)
da=read.table("m-intcsp7309.txt",header=T)
intc=log(da$intc+1)
m4=garchFit(~1+garch(1,1),data=intc,trace=F)
由於有了debugonce(包名::包中的函數)
當執行到m4=garchFit(~1+garch(1,1),data=intc,trace=F)一行的時候,會自動進入調試模式
如果我們還想調試garchFit中的
.garchArgsParser
函數和
.garchFit
函數,只要在運行前寫上即可(上面的代碼塊中已經注釋掉了)
debugonce() sets a one-shot breakpoint–that is, the function will enter the debugger the very next time it runs, but not after that. If you want to debug a function every time it executes, call debug(...) on the function. Call undebug(...) on the function when you no longer want to debug the function each time it executes.
debugonce(x)設置后,一旦x函數運行,就會進入調試器。
——(只執行一次)
如果想要x函數每次執行都進入調試器,那么只要在想要這么做的地方的最開始放上
debug(x),末尾處放上
undebug(x),那么他們之間的代碼塊,只要遇上x函數被執行,都會進入調試器
——(執行多次)
debug(x)
代碼塊
undebug(x)
-------------------------------------------------------------------------------------------
If you’re diagnosing a specific error, you can have RStudio halt execution at the point where the error is raised. To do this, go to Debug -> On Error and change the value from “Error Inspector” to “Break in Code”.
讓Rstudio在發生錯誤的那一行代碼處停止執行。

只需要在debug——on error——選擇break in code
可是我修改之后然並卵啊,試驗了很久都沒倒騰出來....
存疑,歡迎指正!
To keep the the debugger from being invoked whenever any error anywhere happens,
RStudio does not invoke the debugger if it looks like none of your own code is on the stack. If you find that this is excluding an error you want to catch, go to Tools -> Global Options and uncheck “Use debug error handler only when my code contains errors”.
options(error = browser())
test <- 0
for(i in 1:9){
plot("abc")
j <- i+2
test[i+1] <- test[i]+3
k <- i
}

取消這個打鈎(且有勾選break in code),上述的代碼運行完,會自動進入調試模式。
options(error = browser()) ——設置完后,任意error都會進入調試模式
options(error = NULL) ——還原上一個命令,不然每個錯誤都進入太累了
-------------------------------------------------------------------------------------------
調試器:

n or Enter
F10
Execute next statement 可以按下回車鍵,直接運行下一步
s
Shift+F4
Step into function 即第二個按鈕,進入函數
f
Shift+F6
Finish function/loop 結束該函數或循環
c
Shift+F5
Continue running 繼續執行直到下一個斷點
Q
Shift+F8
Stop debugging 停止調試
那么先到這里,這部分太基礎了,之后我結合Rstudio的調試器以fGarch模型中的garchFit函數具體來說明更實用的調試過程,這樣有助於我們高效地查看源碼,我感受到了老司機的召喚!
本文參考: