{"id":5162,"date":"2015-04-27T02:17:58","date_gmt":"2015-04-27T01:17:58","guid":{"rendered":"http:\/\/emilkirkegaard.dk\/en\/?p=5162"},"modified":"2015-04-27T02:17:58","modified_gmt":"2015-04-27T01:17:58","slug":"error-handling-in-r-is-odd","status":"publish","type":"post","link":"https:\/\/emilkirkegaard.dk\/en\/2015\/04\/error-handling-in-r-is-odd\/","title":{"rendered":"Error handling in R is odd"},"content":{"rendered":"<p>So you want to run some code that may throw an error? This is somewhat less common with R than with e.g. php.<\/p>\n<p>It is quite simple <a href=\"https:\/\/docs.python.org\/2\/tutorial\/errors.html\">in Python<\/a>:<\/p>\n<blockquote><p>The <a class=\"reference internal\" href=\"https:\/\/docs.python.org\/2\/reference\/compound_stmts.html#try\"><tt class=\"xref std std-keyword docutils literal\"><span class=\"pre\">try<\/span><\/tt><\/a> statement works as follows.<\/p>\n<ul class=\"simple\">\n<li>First, the <em>try clause<\/em> (the statement(s) between the <a class=\"reference internal\" href=\"https:\/\/docs.python.org\/2\/reference\/compound_stmts.html#try\"><tt class=\"xref std std-keyword docutils literal\"><span class=\"pre\">try<\/span><\/tt><\/a> and <a class=\"reference internal\" href=\"https:\/\/docs.python.org\/2\/reference\/compound_stmts.html#except\"><tt class=\"xref std std-keyword docutils literal\"><span class=\"pre\">except<\/span><\/tt><\/a> keywords) is executed.<\/li>\n<li>If no exception occurs, the <em>except clause<\/em> is skipped and execution of the <a class=\"reference internal\" href=\"https:\/\/docs.python.org\/2\/reference\/compound_stmts.html#try\"><tt class=\"xref std std-keyword docutils literal\"><span class=\"pre\">try<\/span><\/tt><\/a> statement is finished.<\/li>\n<li>If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then if its type matches the exception named after the <a class=\"reference internal\" href=\"https:\/\/docs.python.org\/2\/reference\/compound_stmts.html#except\"><tt class=\"xref std std-keyword docutils literal\"><span class=\"pre\">except<\/span><\/tt><\/a> keyword, the except clause is executed, and then execution continues after the <a class=\"reference internal\" href=\"https:\/\/docs.python.org\/2\/reference\/compound_stmts.html#try\"><tt class=\"xref std std-keyword docutils literal\"><span class=\"pre\">try<\/span><\/tt><\/a> statement.<\/li>\n<li>If an exception occurs which does not match the exception named in the except clause, it is passed on to outer <a class=\"reference internal\" href=\"https:\/\/docs.python.org\/2\/reference\/compound_stmts.html#try\"><tt class=\"xref std std-keyword docutils literal\"><span class=\"pre\">try<\/span><\/tt><\/a> statements; if no handler is found, it is an <em>unhandled exception<\/em> and execution stops with a message as shown above.<\/li>\n<\/ul>\n<\/blockquote>\n<p>Let&#8217;s try something simple in iPython:<\/p>\n<pre>In [2]: try:\r\n\u00a0\u00a0 ...:\u00a0\u00a0\u00a0\u00a0 \"string\"\/7\r\n\u00a0\u00a0 ...: except:\r\n\u00a0\u00a0 ...:\u00a0\u00a0\u00a0\u00a0 print(\"Can't divide a string\")\r\n\u00a0\u00a0 ...:\r\nCan't divide a string<\/pre>\n<p>Simple stuff.<\/p>\n<p>Now, let&#8217;s do the same in R:<\/p>\n<pre>tryCatch(\"string\"\/7,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error = function(e) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\"Can't divide a string\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n)<\/pre>\n<p>Notice how I had to add <a href=\"http:\/\/adv-r.had.co.nz\/Functional-programming.html#anonymous-functions\">an anonymous function<\/a>? Well apparently this is how it has to be done in R. The parameter to the function, e, is not even used. It would be better if one could simply do this:<\/p>\n<pre>tryCatch(\"string\"\/7,\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 error = {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(\"Can't divide a string\")\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }\r\n)<\/pre>\n<p>But no, then you get:<\/p>\n<pre id=\"rstudio_console_output\" class=\"GCG52B5BAFB\" tabindex=\"0\">[1] \"Can't divide a string\"\r\n<span class=\"GCG52B5BKEB  ace_constant\">Error in tryCatchOne(expr, names, parentenv, handlers[[1L]]) : \r\n  attempt to apply non-function<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>So you want to run some code that may throw an error? This is somewhat less common with R than with e.g. php. It is quite simple in Python: The try statement works as follows. First, the try clause (the statement(s) between the try and except keywords) is executed. If no exception occurs, the except [&hellip;]<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[2153,2154,1979],"class_list":["post-5162","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-error-handling","tag-python","tag-r","entry"],"_links":{"self":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/5162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/comments?post=5162"}],"version-history":[{"count":1,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/5162\/revisions"}],"predecessor-version":[{"id":5163,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/posts\/5162\/revisions\/5163"}],"wp:attachment":[{"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/media?parent=5162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/categories?post=5162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/emilkirkegaard.dk\/en\/wp-json\/wp\/v2\/tags?post=5162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}