<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/the-fix-is-in-finding-infix-functions-inside-contributed-r-package-utilities-files/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2018/05/29/the-fix-is-in-finding-infix-functions-inside-contributed-r-package-utilities-files/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Regular readers will recall the “<a rel="nofollow" href="https://rud.is/b/2018/04/08/dissecting-r-package-utility-belts/" target="_blank">utility belt</a>” post from back in April of this year. This is a follow-up to a request made asking for a list of all the <code>%</code> infix functions in those files.</p>
<p>We’re going to:</p>
<ul>
<li>collect up all of the sources</li>
<li>parse them</li>
<li>find all the definitions of <code>%</code> infix functions</li>
<li>write them to a file</li>
</ul>
<p>We’ll start by grabbing the data from the previous post and look at it as a refresher:</p>
<pre><code>
library(stringi)
library(tidyverse)

utils &lt;- read_rds(url(&quot;https://rud.is/dl/utility-belt.rds&quot;))

utils
## # A tibble: 1,746 x 13
##    permsissions links owner     group      size month day   year_hr path   date       pkg   fil   file_src             
##  1 -rw-r--r--       0 hornik    users      1658 Jun   05    2016    AHR/R… 2016-06-05 AHR   util… &quot;## \\int f(x)dg(x) …
##  2 -rw-r--r--       0 ligges    users     12609 Dec   13    2016    ALA4R… 2016-12-13 ALA4R util… &quot;## some utility fun…
##  3 -rw-r--r--       0 hornik    users         0 Feb   24    2017    AWR.K… 2017-02-24 AWR.… util… &quot;&quot;                   
##  4 -rw-r--r--       0 ligges    users      4127 Aug   30    2017    Alpha… 2017-08-30 Alph… util… &quot;#\n#' Assign API ke…
##  5 -rw-r--r--       0 ligges    users       121 Jan   19    2017    Amylo… 2017-01-19 Amyl… util… &quot;make_decision &lt;- fu…
##  6 -rw-r--r--       0 herbrandt herbrandt    52 Aug   10    2017    BANES… 2017-08-10 BANE… util… &quot;#' <a rel="nofollow" href="http://twitter.com/importFrom" target="_blank">@importFrom</a> dply…
##  7 -rw-r--r--       0 ripley    users     36977 Jan   06    2015    BEQI2… 2015-01-06 BEQI2 util… &quot;#' \tRemove Redunda…
##  8 -rw-r--r--       0 hornik    users     34198 May   10    2017    BGDat… 2017-05-10 BGDa… util… &quot;# A more memory-eff…
##  9 -rwxr-xr-x       0 ligges    users      3676 Aug   14    2016    BGLR/… 2016-08-14 BGLR  util… &quot;\n readBinMat=funct…
## 10 -rw-r--r--       0 ripley    users      2547 Feb   04    2015    BLCOP… 2015-02-04 BLCOP util… &quot;###################…
## # ... with 1,736 more rows
</code></pre>
<p>Note that we somewhat expected the file source to potentially come in handy at a later date and also expected the need to revisit that post, so the <a rel="nofollow" href="https://rud.is/dl/utility-belt.rds" target="_blank">R data file</a> [←<em>direct link to RDS</em>] included a <code>file_src</code> column.</p>
<p>Now, let’s find all the source files with at least one infix definition, collect them together and parse them so we can do more code spelunking:</p>
<pre><code>
filter(utils, stri_detect_fixed(file_src, &quot;`%&quot;)) %>% # only find sources with infix definitions
  pull(file_src) %>%
  paste0(collapse=&quot;\n\n&quot;) %>%
  parse(text = ., keep.source=TRUE) -> infix_src

str(infix_src, 1)
## length 1364 expression(dplyr::`%>%`, `%||%` &lt;- function(a, b) if (is.null(a)) b else a, get_pkg_path &lt;- function(ctx) {  pkg_| __truncated__ ...
##  - attr(*, &quot;srcref&quot;)=List of 1364
##  - attr(*, &quot;srcfile&quot;)=Classes 'srcfilecopy', 'srcfile' </code></pre>