<h2><img sizes="(max-width: 449px) 100vw, 449px" data-image-description="" data-orig-file="https://longhowlam.files.wordpress.com/2017/04/rp.png?w=449&amp;resize=449%2C461#038;h=461" data-orig-size="730,749" data-image-title="RP" class="wp-image-2210" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="2210" data-permalink="https://longhowlam.wordpress.com/2017/04/10/test-driving-python-integration-in-r-using-the-reticulate-package/rp/" alt="RP" data-medium-file="https://longhowlam.files.wordpress.com/2017/04/rp.png?w=449&amp;resize=449%2C461#038;h=461?w=292" data-large-file="https://longhowlam.files.wordpress.com/2017/04/rp.png?w=449&amp;resize=449%2C461#038;h=461?w=440" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://longhowlam.files.wordpress.com/2017/04/rp.png?w=449&resize=449%2C461#038;h=461"></h2>
<h2>Introduction</h2>
<p>Not so long ago RStudio released the R package ‘<a target="_blank" rel="nofollow" href="http://github.com/rstudio/reticulate">reticulate</a>‘, it is an R interface to Python. Of course, it was already possible to execute python scripts from within R, but this integration takes it one step further. Imported Python modules, classes and functions can be called inside an R session as if it were just native R functions.</p>
<p>Below you’ll find some screen shot code snippets of using certain Python modules within R with the reticulate package. On <a target="_blank" rel="nofollow" href="https://github.com/longhowlam/ReticulateTestDrive">my GitHub page</a> you’ll find the R files from which these snippets were taken from.</p>
<h2>Using python packages</h2>
<p>The nice thing about reticulate in RStudio is the support for code completion. When you have imported a python module, RStudio will recognize the methods that are available in the python module:</p>
<p><img sizes="(max-width: 620px) 100vw, 620px" data-image-description="" data-orig-file="https://longhowlam.files.wordpress.com/2017/04/clarifai_code_comp.png?w=450#038;h=222" data-orig-size="723,258" data-image-title="clarifai_code_comp" data-comments-opened="1" class="wp-image-2213" data-recalc-dims="1" data-attachment-id="2213" data-permalink="https://longhowlam.wordpress.com/2017/04/10/test-driving-python-integration-in-r-using-the-reticulate-package/clarifai_code_comp/" alt="clarifai_code_comp" data-medium-file="https://longhowlam.files.wordpress.com/2017/04/clarifai_code_comp.png?w=450#038;h=222?w=300" data-large-file="https://longhowlam.files.wordpress.com/2017/04/clarifai_code_comp.png?w=450#038;h=222?w=440" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://longhowlam.files.wordpress.com/2017/04/clarifai_code_comp.png?w=450#038;h=222"></p>
<h3>The clarifai module</h3>
<p><a target="_blank" rel="nofollow" href="http://clarifai.com/">Clarifai </a>provides a set of computer vision API’s for image recognition, face detection, extracting tags, etc. There is an official <a target="_blank" rel="nofollow" href="http://github.com/Clarifai/clarifai-python">python module</a> and there is also an R package by <a target="_blank" rel="nofollow" href="http://github.com/soodoku/clarifai">Guarav Sood</a>, but it exposes less functionality. So I am going to use the python module in R. The following code snippet shows how easy it is to call python functions.</p>
<p><img sizes="(max-width: 587px) 100vw, 587px" data-image-description="" data-orig-file="https://longhowlam.files.wordpress.com/2017/04/clarifaicode.png?w=450#038;h=442" data-orig-size="708,534" data-image-title="clarifaicode" data-comments-opened="1" class="wp-image-2215" data-recalc-dims="1" data-attachment-id="2215" data-permalink="https://longhowlam.wordpress.com/2017/04/10/test-driving-python-integration-in-r-using-the-reticulate-package/clarifaicode/" alt="clarifaicode" data-medium-file="https://longhowlam.files.wordpress.com/2017/04/clarifaicode.png?w=450#038;h=442?w=300" data-large-file="https://longhowlam.files.wordpress.com/2017/04/clarifaicode.png?w=450#038;h=442?w=440" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://longhowlam.files.wordpress.com/2017/04/clarifaicode.png?w=450#038;h=442"></p>
<p>The output returned from the clarifai call is a nested list and can be quit intimidating at first sight. To browse trough these nested lists and to get a better idea of what is in those lists, you can use the package <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/listviewer/index.html">listviewer</a>:</p>
<p><img sizes="(max-width: 558px) 100vw, 558px" data-image-description="" data-orig-file="https://longhowlam.files.wordpress.com/2017/04/listviewer.png?w=450#038;h=504" data-orig-size="706,638" data-image-title="listviewer" data-comments-opened="1" class="wp-image-2219" data-recalc-dims="1" data-attachment-id="2219" data-permalink="https://longhowlam.wordpress.com/2017/04/10/test-driving-python-integration-in-r-using-the-reticulate-package/listviewer/" alt="listviewer" data-medium-file="https://longhowlam.files.wordpress.com/2017/04/listviewer.png?w=450#038;h=504?w=300" data-large-file="https://longhowlam.files.wordpress.com/2017/04/listviewer.png?w=450#038;h=504?w=440" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://longhowlam.files.wordpress.com/2017/04/listviewer.png?w=450#038;h=504"></p>
<h3>The pattern.nl module</h3>
<p>The<a target="_blank" rel="nofollow" href="http://www.clips.ua.ac.be/pages/pattern-nl"> pattern.nl module</a> contains a fast part-of-speech tagger for Dutch, sentiment analysis, and tools for Dutch verb conjugation and noun singularization &amp; pluralization. At the moment it does not support python 3. That is not a big deal, I am using Anaconda and created a Python 2.7 environment to install pattern.nl.</p>
<p>The nice thing of the reticulate package is that it allows you to choose a specific Python environment to be used.</p>
<p><img sizes="(max-width: 594px) 100vw, 594px" data-image-description="" data-orig-file="https://longhowlam.files.wordpress.com/2017/04/pattern.png?w=450#038;h=333" data-orig-size="872,488" data-image-title="pattern" data-comments-opened="1" class="wp-image-2221" data-recalc-dims="1" data-attachment-id="2221" data-permalink="https://longhowlam.wordpress.com/2017/04/10/test-driving-python-integration-in-r-using-the-reticulate-package/pattern/" alt="pattern" data-medium-file="https://longhowlam.files.wordpress.com/2017/04/pattern.png?w=450#038;h=333?w=300" data-large-file="https://longhowlam.files.wordpress.com/2017/04/pattern.png?w=450#038;h=333?w=440" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://longhowlam.files.wordpress.com/2017/04/pattern.png?w=450#038;h=333"></p>
<h3>The pytorch module</h3>
<p><a target="_blank" rel="nofollow" href="http://pytorch.org/">pytorch </a>is a python package that provides tensor computations and deep neural networks. There is no ‘R torch’ equivalent, but we can use reticulate in R. There is an example of training a logistic regression in pytorch, <a target="_blank" rel="nofollow" href="http://github.com/yunjey/pytorch-tutorial/blob/master/tutorials/02%20-%20Logistic%20Regression/main.py#L35-L42">see the code here</a>. It takes just a little rewrite of this code to make this work in R. See the first few lines in the figure below.</p>
<p><img sizes="(max-width: 612px) 100vw, 612px" data-image-description="" data-orig-file="https://longhowlam.files.wordpress.com/2017/04/pytorch.png?w=450#038;h=306" data-orig-size="996,498" data-image-title="pytorch" data-comments-opened="1" class="wp-image-2224" data-recalc-dims="1" data-attachment-id="2224" data-permalink="https://longhowlam.wordpress.com/2017/04/10/test-driving-python-integration-in-r-using-the-reticulate-package/pytorch/" alt="pytorch" data-medium-file="https://longhowlam.files.wordpress.com/2017/04/pytorch.png?w=450#038;h=306?w=300" data-large-file="https://longhowlam.files.wordpress.com/2017/04/pytorch.png?w=450#038;h=306?w=440" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://longhowlam.files.wordpress.com/2017/04/pytorch.png?w=450#038;h=306"></p>
<h2>Conclusion</h2>
<p>As a data scientist you should know both <strong>R and Python</strong>, the reticulate package is no excuse for not learning Python! However, the reticulate package can be very useful if you want to do all your analysis in the RStudio environment. It works very well.</p>
<p>For example, I have used <a target="_blank" rel="nofollow" href="https://github.com/hadley/rvest">rvest </a>to scrape some Dutch news texts, then used the Python module <a target="_blank" rel="nofollow" href="http://github.com/clips/pattern">pattern.nl</a> for Dutch sentiment and wrote an <a target="_blank" rel="nofollow" href="http://rmarkdown.rstudio.com/">R Markdown</a> document to present the results. Then the reticulate package is a nice way to keep everything in one environment.</p>
<p>Cheers, Longhow</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/longhowlam.wordpress.com/2208/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/longhowlam.wordpress.com/2208/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/H89byYT1Wxo">