{"id":219,"date":"2011-05-31T04:33:56","date_gmt":"2011-05-31T08:33:56","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/?p=219"},"modified":"2011-05-31T04:33:56","modified_gmt":"2011-05-31T08:33:56","slug":"r%e4%bb%a3%e7%a0%81%e9%99%a4%e9%94%99-how-to-debug-r-code","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2011\/05\/r%e4%bb%a3%e7%a0%81%e9%99%a4%e9%94%99-how-to-debug-r-code\/","title":{"rendered":"R\u4ee3\u7801\u9664\u9519 (How to debug R code)"},"content":{"rendered":"<p>R\u4ee3\u7801\u9664\u9519 (How to debug R code)<br \/>\nTricks about how to debug R code<\/p>\n<p>\u4f7f\u7528R\u7684\u7528\u6237\u4e2d\u5f88\u591a\u4eba\u62b1\u6028R\u7684\u4ee3\u7801\u4e0d\u597d\u8c03\u8bd5\u3002\u5bf9\u6211\u6765\u8bf4\uff0c\u6211\u89c9\u5f97R\u81f3\u5c11\u6bd4Perl\u597d\u4e00\u70b9\uff0c\u56e0\u4e3a\u81f3\u5c11R\u7684\u8bf4\u660e\u6863\u4e30\u5bcc\uff0c\u81f3\u5c11\u770b\u7684\u61c2\u6e90\u7801\u3002\u597d\u4e86\uff0c\u957f\u8bdd\u77ed\u8bf4\uff0cR\u7684\u754c\u9762\u5f88\u7b80\u5355\uff0c\u6ca1\u6709Visual studio\u90a3\u4e48\u5f3a\u5927\u7684\u8c03\u8bd5\u5668\uff0c\u4e5f\u6ca1\u6709GDB\u90a3\u6837\u7075\u6d3b\u7684\u8c03\u8bd5\u547d\u4ee4\uff08\u89c1<a href=\"http:\/\/zhanxw.com\/blog\/2011\/03\/gdb-%e4%bd%bf%e7%94%a8%e7%bb%8f%e9%aa%8c\/\"> GDB \u4f7f\u7528\u7ecf\u9a8c<\/a>, <a href=\"http:\/\/zhanxw.com\/blog\/2011\/03\/gdb-%e4%bd%bf%e7%94%a8%e7%bb%8f%e9%aa%8c%ef%bc%88%e4%ba%8c%ef%bc%89\/\"> GDB \u4f7f\u7528\u7ecf\u9a8c\uff08\u4e8c\uff09<\/a>\uff09\uff0c\u6211\u603b\u7ed3\u51fa\u6765\u4ee5\u4e0b5\u79cd\u8c03\u8bd5\u65b9\u6cd5\uff0c\u7528\u5728\u4e0d\u540c\u7684\u573a\u5408\u3002\u5f53\u7136\u8bdd\u8bf4\u56de\u6765\uff0c\u8fd8\u662f\u5c3d\u91cf\u5199\u6ca1\u6709bug\u7684\u4ee3\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u3002<\/p>\n<p>1. \u4f20\u7edf\u8c03\u8bd5\u51fd\u6570<br \/>\ntraceback(), debug(), trace(), browser(), recover()<br \/>\ntraceback()  \u662f\u5728\u51fa\u9519\u9000\u51fa\u540e\uff0c\u6253\u5370\u51fa\u8c03\u7528\u5806\u6808\u7684\u60c5\u51b5<br \/>\ndebug() \u662f\u5c06\u65ad\u70b9\u8bbe\u7f6e\u5728\u4e00\u4e2a\u51fd\u6570\u4e0a\uff0c\u8fd9\u4e2a\u51fd\u6570\u88ab\u8c03\u7528\u7684\u65f6\u5019\u4f1a\u53d8\u4e3a\u5355\u6b65\u6267\u884c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u8ddf\u8e2a\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u4e0d\u5982gdb\u7075\u6d3b<br \/>\ntrace() \u7b49\u4e8e\u662f\u5728\u51fd\u6570\u4e2d\u63d2\u5165\u989d\u5916\u7684\u8c03\u8bd5\u4ee3\u7801\uff0c\u4f8b\u5982\uff1atrace(sum)\u5728\u6bcf\u6b21\u8c03\u7528sum\u7684\u65f6\u5019\u6253\u5370\u51fasum\u7684\u53c2\u6570\uff1b\u53c8\u6bd4\u5982<br \/>\n## arrange to call the browser on entering and exiting<br \/>\n## function f<br \/>\ntrace(&#8220;f&#8221;, quote(browser(skipCalls=4)), exit = quote(browser(skipCalls=4)))<br \/>\n\u5219\u8868\u793a\u4f7f\u7528browser()\u6765\u8c03\u8bd5\uff0c\u4ece\u7b2c5\u6b21\u5f00\u59cb<br \/>\nbrowser()\uff1a\u8fd9\u4e2a\u51fd\u6570\u5f80\u5f80\u4f5c\u4e3a\u53c2\u6570\uff0c\u88ab\u8c03\u7528\u65f6\u7528\u6237\u53ef\u4ee5\u68c0\u67e5\u53d8\u91cf\u3002\u7528\u6237\u53ef\u4ee5\u8f93\u5165c\u8868\u793a\u7ee7\u7eed\uff0cn\u8868\u793a\u4e0b\u4e00\u6761\u6307\u4ee4\uff0cQ\u8868\u793a\u9000\u51fa<br \/>\nrecover()\uff1a\u548cbrowser\u7c7b\u4f3c\uff0c\u4e5f\u662f\u88ab\u8c03\u7528\u3002\u4e0d\u540c\u5728\u4e8e\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684frame\uff08\u5806\u6808\u6df1\u5ea6\uff09\u3002<\/p>\n<p>2. \u66f4\u4f20\u7edf\u7684\u8c03\u8bd5\u51fd\u6570print()\uff0ccat()<br \/>\n\u4f7f\u7528print()\u6765\u6253\u5370\u6bcf\u4e2a\u53d8\u91cf\u8c03\u7528\u65f6\u5019\u7684\u503c\uff1b<br \/>\n\u66f4\u7b80\u5355\u7684\u60c5\u51b5\u53ef\u4ee5\u7528cat()\uff0c\u5b83\u7684\u8bed\u6cd5\u66f4\u7b80\u5355\uff0c\u4f8b\u5982cat(&#8220;x=&#8221;, x)<\/p>\n<p>3. \u8bbe\u7f6eoptions(error=&#8230;)<br \/>\n\u6211\u4eec\u5e0c\u671b\u51fa\u9519\u7684\u65f6\u5019\uff0cR\u53ef\u4ee5\u505c\u6b62\u6267\u884c\u540e\u7eed\u4ee3\u7801\uff0c\u5e76\u8fdb\u5165\u6211\u4eec\u6307\u5b9a\u7684\u8c03\u5f0f\u6a21\u5f0f\u3002<br \/>\n\u5728R\u7684\u4ea4\u4e92\u754c\u9762\uff0c\u53ef\u4ee5\u8bbe\u7f6e\uff1a<br \/>\noptions(error=recover)<br \/>\n\u5728Rscript\uff0c\u5373\u547d\u4ee4\u884c\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7528\u4e0b\u9762\u7684\u8bdd\u628a\u51fa\u9519\u4fe1\u606f\u5b58\u50a8\u5230\u6587\u4ef6\uff1a<br \/>\noptions(error = quote({dump.frames(to.file=TRUE); q()}))<\/p>\n<p>\u8c03\u8bd5\u5b8c\u6bd5\uff0c\u6062\u590d\u521d\u59cb\u8bbe\u7f6e\u65f6\uff0c\u53ef\u4ee5\u7528\uff1a<br \/>\noptions(error = NULL)<\/p>\n<p>\u8fd9\u91cc\u4e3e\u4e2a\u4f8b\u5b50\u5427\uff08<a href=\"http:\/\/stackoverflow.com\/questions\/1412582\/getting-the-state-of-variables-after-an-error-occurs-in-r\"> \u51fa\u5904<\/a>\uff09\uff1a<br \/>\n\u9519\u8bef\u7684\u60c5\u666f\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nx &lt;- 1:5\r\ny &lt;- x + rnorm(length(x),0,1)\r\nf &lt;- function(x,y) {\r\n  y &lt;- c(y,1)\r\n  lm(y~x)\r\n}\r\n<\/pre>\n<p>\u6211\u4eec\u8c03\u8bd5\u7684\u65f6\u5019\uff0c\u8f93\u5165\uff1a<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\noptions(error=recover)\r\n\r\n&gt; f(x,y)\r\nError in model.frame.default(formula = y ~ x, drop.unused.levels = TRUE) : \r\n  variable lengths differ (found for 'x')\r\n\r\nEnter a frame number, or 0 to exit   \r\n\r\n1: f(x, y)\r\n2: lm(y ~ x)\r\n3: eval(mf, parent.frame())\r\n4: eval(expr, envir, enclos)\r\n5: model.frame(formula = y ~ x, drop.unused.levels = TRUE)\r\n6: model.frame.default(formula = y ~ x, drop.unused.levels = TRUE)\r\n\r\nSelection: 1\r\nCalled from: eval(expr, envir, enclos)\r\nBrowse&#x5B;1]&gt; x\r\n&#x5B;1] 1 2 3 4 5\r\nBrowse&#x5B;1]&gt; y\r\n&#x5B;1] 1.6591197 0.5939368 4.3371049 4.4754027 5.9862130 1.0000000\r\n<\/pre>\n<p>\u901a\u8fc7\u68c0\u67e5x\u548cy\u7684\u503c\u5c31\u80fd\u53d1\u73b0\u95ee\u9898\u4e86\u3002<\/p>\n<p>4. \u8bbe\u7f6e\u65ad\u70b9 setBreakpoint()<br \/>\n\u4eceR 2.10\u5f00\u59cb\uff0c\u6211\u4eec\u6709\u4e86\u4e24\u4e2a\u8c03\u8bd5\u76f8\u5173\u7684\u51fd\u6570findLineNum(), setBreakpoint()<br \/>\n\u6709\u4e86\u65ad\u70b9\uff0c\u6211\u4eec\u53ef\u4ee5\u5feb\u901f\u6267\u884c\u4ee3\u7801\uff0c\u76f4\u81f3\u6709\u53ef\u80fd\u7684\u9519\u8bef\u90e8\u5206\uff08\u60f3\u60f3\u5982\u679c\u53ea\u6709debug()\u5219\u9700\u8981\u4eba\u5de5\u5355\u6b65\u6267\u884cR\u8bed\u53e5\uff0c\u6216\u8005\u9519\u8bef\u53d1\u751f\u540erecover()\uff0c\u6211\u4eec\u9700\u8981\u53cd\u63a8\u5230\u5e95\u662f\u4ec0\u4e48\u9020\u6210\u7684\u9519\u8bef\uff09\u3002\u8fd9\u5c06\u5927\u5927\u63d0\u9ad8\u6211\u4eec\u9664\u9519\u7684\u901f\u5ea6\u3002<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/1445964\/r-script-line-numbers-at-error\"> \u51fa\u5904<\/a><\/p>\n<p>\u8fd9\u91cc\u4e3e\u4e2a\u4f8b\u5b50\u5c55\u793a\u5982\u4f55\u5728\u7b2c3\u884c\u8bbe\u7f6e\u65ad\u70b9\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nx &lt;- &quot; f &lt;- function(a, b) {\r\n             if (a &gt; b)  {\r\n                 a\r\n             } else {\r\n                 b\r\n             }\r\n         }&quot;\r\n\r\n\r\neval(parse(text=x))  # Normally you'd use source() to read a file...\r\n\r\nfindLineNum(&quot;&lt;text&gt;#3&quot;)   # &lt;text&gt; is a dummy filename used by parse(text=)\r\n\r\n#This will print\r\n#f step 2,3,2 in &lt;environment: R_GlobalEnv&gt;\r\n\r\n#and you can use\r\n\r\nsetBreakpoint(&quot;&lt;text&gt;#3&quot;)\r\n<\/pre>\n<p>5. *apply \u51fd\u6570\u4e2d\u5982\u4f55\u8c03\u8bd5\uff1a<br \/>\n\u7528\u8fc7R\u7684\u90fd\u77e5\u9053\u5728\u5faa\u73af\u4e2d\u51fa\u9519\u4e0d\u5bb9\u6613\u3002\u56e0\u4e3aR\u5904\u7406\u5faa\u73af\u5f88\u6162\uff0c\u6211\u4eec\u5f80\u5f80\u4e0d\u7528for\u5faa\u73af\uff0c\u800c\u7528sapply(), lapply()\u7b49\u7b49\u3002\u8fd9\u4e9b\u51fd\u6570\u51fa\u9519\u7684\u65f6\u5019\u4ece\u6765\u4e0d\u4f1a\u8bf4\u662f\u7b2c\u51e0\u4e2a\u5faa\u73af\u53d8\u91cf\u51fa\u9519\u7684\u3002\u5bf9\u6b64\uff0c\u6211\u4eec\u6709\u5982\u4e0b\u65b9\u6cd5\uff1a<\/p>\n<p>\u4f7f\u7528try()\u51fd\u6570, <a href=\"http:\/\/stackoverflow.com\/questions\/1395622\/debugging-lapply-sapply-calls\"> \u51fa\u5904 <\/a>\uff1a<br \/>\n\u4e3e\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n&gt; x &lt;- as.list(-2:2)\r\n&gt; x&#x5B;&#x5B;2]] &lt;- &quot;what?!?&quot;\r\n&gt; ## using sapply\r\n&gt; sapply(x, function(x) 1\/x)\r\nError in 1\/x : non-numeric argument to binary operator\r\n# \u770b\u770b\u7528try()\u51fd\u6570\u600e\u4e48\u6837\uff1f\r\n&gt; sapply(x, function(x) try(1\/x))\r\nError in 1\/x : non-numeric argument to binary operator\r\n&#x5B;1] &quot;-0.5&quot;                                                    \r\n&#x5B;2] &quot;Error in 1\/x : non-numeric argument to binary operator\\n&quot;\r\n&#x5B;3] &quot;Inf&quot;                                                     \r\n&#x5B;4] &quot;1&quot;                                                       \r\n&#x5B;5] &quot;0.5&quot;\r\n<\/pre>\n<p>\u6216\u8005\u7b2c\u4e09\u65b9\u7a0b\u5e8f\u5e93\u4e5f\u884c\uff1a<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/1882734\/what-is-your-favorite-r-debugging-trick\"> \u51fa\u5904<\/a><br \/>\nforeach(.verbose= TRUE)  \u2014\u2014 \u8fd9\u4e2a\u6211\u6ca1\u8bd5\u9a8c\u51fa\u6765\uff0c\u4e0d\u8fc7foreach\u4ecd\u7136\u662f\u4e2a\u5f3a\u5927\u7684\u5de5\u5177<br \/>\nplyr(.inform=TRUE)<br \/>\n\u7ed9\u4e2aplyr\u5e93\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n&gt; laply(x, function(x) 1\/x, .inform = TRUE)\r\n\r\nError in 1\/x : non-numeric argument to binary operator\r\nError: with piece 2: \r\n&#x5B;1] &quot;what?&quot;\r\n<\/pre>\n<p>\u53e6\u5916\u9898\u5916\u8bdd\uff0cR\u91cc\u9762\u6267\u884cinstall.packages()\u7684\u65f6\u5019\uff0c\u53ea\u6709\u5934\u4e00\u6b21\u53ef\u4ee5\u9009repo\uff08\u955c\u50cf\u5e93\uff09\u7684\u4f4d\u7f6e\uff0c\u5982\u679c\u4e4b\u540e\u4f60\u8fd8\u60f3\u9009\u4e0d\u540c\u7684\u955c\u50cf\u5e93\u600e\u4e48\u529e\uff1f\u53ef\u4ee5\u6267\u884c\u8fd9\u4e2a\uff1a<br \/>\noptions(&#8220;repos&#8221;=c(CRAN=&#8221;@CRAN@&#8221;))<\/p>\n<p>\u6700\u540e\u628a\u53c2\u8003\u8fc7\u7684\u7f51\u9875\u5217\u5728\u4e0b\u9762\uff1a<br \/>\n\u30101\u3011<a href=\"http:\/\/stackoverflow.com\/questions\/1412582\/getting-the-state-of-variables-after-an-error-occurs-in-r\">Getting the state of variables after an error occurs in R<\/a><br \/>\n\u30102\u3011<a href=\"http:\/\/stackoverflow.com\/questions\/1882734\/what-is-your-favorite-r-debugging-trick\">What is your favorite R debugging trick?<\/a><br \/>\n\u30103\u3011<a href=\"http:\/\/stackoverflow.com\/questions\/1395622\/debugging-lapply-sapply-calls\">Debugging lapply\/sapply calls<\/a><br \/>\n\u30104\u3011<a href=\"http:\/\/stackoverflow.com\/questions\/1445964\/r-script-line-numbers-at-error\">R script line numbers at error?<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>R\u4ee3\u7801\u9664\u9519 (How to debug R code) Tricks about how to debug R code \u4f7f\u7528R\u7684\u7528\u6237\u4e2d\u5f88\u591a\u4eba\u62b1\u6028R\u7684\u4ee3\u7801\u4e0d\u597d\u8c03\u8bd5\u3002\u5bf9\u6211\u6765\u8bf4\uff0c\u6211\u89c9\u5f97R\u81f3\u5c11\u6bd4Perl\u597d\u4e00\u70b9\uff0c\u56e0\u4e3a\u81f3\u5c11R\u7684\u8bf4\u660e\u6863\u4e30\u5bcc\uff0c\u81f3\u5c11\u770b\u7684\u61c2\u6e90\u7801\u3002\u597d\u4e86\uff0c\u957f\u8bdd\u77ed\u8bf4\uff0cR\u7684\u754c\u9762\u5f88\u7b80\u5355\uff0c\u6ca1\u6709Visual studio\u90a3\u4e48\u5f3a\u5927\u7684\u8c03\u8bd5\u5668\uff0c\u4e5f\u6ca1\u6709GDB\u90a3\u6837\u7075\u6d3b\u7684\u8c03\u8bd5\u547d\u4ee4\uff08\u89c1 GDB \u4f7f\u7528\u7ecf\u9a8c, GDB \u4f7f\u7528\u7ecf\u9a8c\uff08\u4e8c\uff09\uff09\uff0c\u6211\u603b\u7ed3\u51fa\u6765\u4ee5\u4e0b5\u79cd\u8c03\u8bd5\u65b9\u6cd5\uff0c\u7528\u5728\u4e0d\u540c\u7684\u573a\u5408\u3002\u5f53\u7136\u8bdd\u8bf4\u56de\u6765\uff0c\u8fd8\u662f\u5c3d\u91cf\u5199\u6ca1\u6709bug\u7684\u4ee3\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u3002 1. \u4f20\u7edf\u8c03\u8bd5\u51fd\u6570 traceback(), debug(), trace(), browser(), recover() traceback() \u662f\u5728\u51fa\u9519\u9000\u51fa\u540e\uff0c\u6253\u5370\u51fa\u8c03\u7528\u5806\u6808\u7684\u60c5\u51b5 debug() \u662f\u5c06\u65ad\u70b9\u8bbe\u7f6e\u5728\u4e00\u4e2a\u51fd\u6570\u4e0a\uff0c\u8fd9\u4e2a\u51fd\u6570\u88ab\u8c03\u7528\u7684\u65f6\u5019\u4f1a\u53d8\u4e3a\u5355\u6b65\u6267\u884c\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u624b\u52a8\u8ddf\u8e2a\uff0c\u53ea\u4e0d\u8fc7\u8fd9\u91cc\u4e0d\u5982gdb\u7075\u6d3b trace() \u7b49\u4e8e\u662f\u5728\u51fd\u6570\u4e2d\u63d2\u5165\u989d\u5916\u7684\u8c03\u8bd5\u4ee3\u7801\uff0c\u4f8b\u5982\uff1atrace(sum)\u5728\u6bcf\u6b21\u8c03\u7528sum\u7684\u65f6\u5019\u6253\u5370\u51fasum\u7684\u53c2\u6570\uff1b\u53c8\u6bd4\u5982 ## arrange to call the browser on entering and exiting ## function f trace(&#8220;f&#8221;, quote(browser(skipCalls=4)), exit = quote(browser(skipCalls=4))) \u5219\u8868\u793a\u4f7f\u7528browser()\u6765\u8c03\u8bd5\uff0c\u4ece\u7b2c5\u6b21\u5f00\u59cb browser()\uff1a\u8fd9\u4e2a\u51fd\u6570\u5f80\u5f80\u4f5c\u4e3a\u53c2\u6570\uff0c\u88ab\u8c03\u7528\u65f6\u7528\u6237\u53ef\u4ee5\u68c0\u67e5\u53d8\u91cf\u3002\u7528\u6237\u53ef\u4ee5\u8f93\u5165c\u8868\u793a\u7ee7\u7eed\uff0cn\u8868\u793a\u4e0b\u4e00\u6761\u6307\u4ee4\uff0cQ\u8868\u793a\u9000\u51fa recover()\uff1a\u548cbrowser\u7c7b\u4f3c\uff0c\u4e5f\u662f\u88ab\u8c03\u7528\u3002\u4e0d\u540c\u5728\u4e8e\u7528\u6237\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684frame\uff08\u5806\u6808\u6df1\u5ea6\uff09\u3002 2. \u66f4\u4f20\u7edf\u7684\u8c03\u8bd5\u51fd\u6570print()\uff0ccat() [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,15],"tags":[57,45],"class_list":["post-219","post","type-post","status-publish","format-standard","hentry","category-code","category-statistics","tag-debug","tag-r"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/219","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}