{"id":17983,"date":"2021-01-26T09:03:54","date_gmt":"2021-01-26T09:03:54","guid":{"rendered":"http:\/\/www.wjst.de\/blog\/?p=17983"},"modified":"2022-02-08T17:15:58","modified_gmt":"2022-02-08T15:15:58","slug":"breaking-up-long-chains-in-rs-dplyr-magrittr-code-and-calling-sub-functions","status":"publish","type":"post","link":"https:\/\/www.wjst.de\/blog\/sciencesurf\/2021\/01\/breaking-up-long-chains-in-rs-dplyr-magrittr-code-and-calling-sub-functions\/","title":{"rendered":"Breaking up long chains in R&#8217;s dplyr\/magrittr code and calling sub functions"},"content":{"rendered":"<p>Having several long and redundant chains in R &#8216;s magittr code, I have now figured out how to pipe into named and unnamed functions<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\nf1 &lt;- function(x) {\r\n  x %&gt;% count() %&gt;% print()\r\n}\r\nf2 &lt;- function(x) {\r\n  x %&gt;% tibble() %&gt;% print()\r\n}\r\n<\/pre>\n<p>So we have now two named functions with code blocks that can be inserted in an unnamed function whenever needed<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\niris %&gt;%\r\n  # do any select, mutate ....\r\n  # before running both functions\r\n  (function(x) {\r\n    x %&gt;% select(Species) %&gt;% f1() %&gt;% print()\r\n    x %&gt;% f2() %&gt;% print()\r\n  })\r\n<\/pre>\n<p>We can make a variable global from inside a function using &lt;&lt;- assignment but I have not found a way to continue the dplyr chain with any returned variable. So we are trapped inside the function<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\niris %&gt;%\r\n  (function(x) {\r\n    result &lt;&lt;- x %&gt;% head(1)\r\n  })\r\n<\/pre>\n\n<p>&nbsp;<\/p>\n<div class=\"bottom-note\">\n  <span class=\"mod1\">CC-BY-NC Science Surf , accessed 06.06.2026<\/span>\n <\/div>","protected":false},"excerpt":{"rendered":"<p>Having several long and redundant chains in R &#8216;s magittr code, I have now figured out how to pipe into named and unnamed functions f1 &lt;- function(x) { x %&gt;% count() %&gt;% print() } f2 &lt;- function(x) { x %&gt;% tibble() %&gt;% print() } So we have now two named functions with code blocks that &hellip; <a href=\"https:\/\/www.wjst.de\/blog\/sciencesurf\/2021\/01\/breaking-up-long-chains-in-rs-dplyr-magrittr-code-and-calling-sub-functions\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Breaking up long chains in R&#8217;s dplyr\/magrittr code and calling sub functions<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2893],"class_list":["post-17983","post","type-post","status-publish","format-standard","hentry","category-computer-software","tag-r"],"_links":{"self":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts\/17983","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/comments?post=17983"}],"version-history":[{"count":10,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts\/17983\/revisions"}],"predecessor-version":[{"id":19231,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/posts\/17983\/revisions\/19231"}],"wp:attachment":[{"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/media?parent=17983"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/categories?post=17983"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wjst.de\/blog\/wp-json\/wp\/v2\/tags?post=17983"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}