<p>I’ve been using the <a rel="nofollow" href="https://github.com/rstudio/reticulate" target="_blank">reticulate</a> package occasionally for a while now, so I was surprised to see that it had only just been officially released. </p>
<blockquote class="twitter-tweet" data-lang="en">
<p lang="en" dir="ltr">reticulate: R interface to Python <a rel="nofollow" href="https://t.co/qVWmwoMQAP" target="_blank">https://t.co/qVWmwoMQAP</a>. Comprehensive set of interoperability tools including R Markdown Python engine <a rel="nofollow" href="https://twitter.com/hashtag/rstats?src=hash&ref_src=twsrc%5Etfw" target="_blank">#rstats</a> <a rel="nofollow" href="https://twitter.com/hashtag/pydata?src=hash&ref_src=twsrc%5Etfw" target="_blank">#pydata</a> <a rel="nofollow" href="https://t.co/SuWM6Y3Pk0" target="_blank">pic.twitter.com/SuWM6Y3Pk0</a></p>
<p>— RStudio (@rstudio) <a rel="nofollow" href="https://twitter.com/rstudio/status/978293244390985728?ref_src=twsrc%5Etfw" target="_blank">March 26, 2018</a></p>
</blockquote>
<p></p>
<p><span></span></p>
<p>It’s a brilliant piece of work, allowing python and R to coexist in the same workflow. </p>
<p>Another opportunity came up today to use it so I thought it might be nice to do a very quick blog post to show just how easy it is to take external python code and have it callable directly from R. In this case, <a rel="nofollow" href="https://twitter.com/coolbutuseless" target="_blank">@coolbutuseless</a> posed a challenge on Twitter to write a fast ‘needle in a haystack’ search of a small vector inside a larger one. I looked over the existing candidates and figured some sort of <a rel="nofollow" href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" target="_blank">Sieve of Eratosthenes</a>-esque algorithm might have a chance (though the name eluded me entirely at the time). </p>
<p>My proposal was to search for the first digit using <code>which()</code>, and use this reduced vector of possible-matches in additional tests on the remaining parts of the ‘needle’. <a rel="nofollow" href="https://twitter.com/coolbutuseless" target="_blank">@coolbutuseless</a> refactored my attempt allowing for arbitrary length needles and found it to <a rel="nofollow" href="https://coolbutuseless.bitbucket.io/2018/04/03/finding-a-length-n-needle-in-a-haystack/" target="_blank">do quite well against the current offerings</a>. What he still wanted though was a <a rel="nofollow" href="https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm" target="_blank">Boyer–Moore string search algorithm</a> implementation. This is apparently what <a rel="nofollow" href="https://lists.freebsd.org/pipermail/freebsd-current/2010-August/019310.html" target="_blank">GNU <code>grep</code> uses</a>, so it’s probably pretty okay.</p>
<p>That algorithm is pretty clever about how it goes about the search, starting in a similar way to what I did (the sieve approach was apparently the leading string match method prior to Boyer-Moore). It’s much more complicated though, so I wasn’t about to write one of those myself in R. Nowadays, people think of C/C++ when there’s functionality they want to grab from elsewhere. There’s a C implementation on the Wikipedia site, so that seems like a nice place to start. I <a rel="nofollow" href="https://gist.github.com/jonocarroll/d658b5ccf33aaef150b6b36f055d2d6d#file-boyermoore-c" target="_blank">saved the text</a> to a new <code>boyermoore.c</code> file and ran </p>
<p><code>R CMD SHLIB boyermoore.c</code></p>
<p>from a terminal to compile it into <code>boyermore.so</code>. This could then be loaded into R with <code>dyn.load(&quot;boyermore.so&quot;)</code> and in theory called with <code>.C(&quot;boyer_moore&quot;, </code>