<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/r-or-python-why-not-both-using-anaconda-python-within-r-with-reticulate/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.brodrigues.co/blog/2018-12-30-reticulate/"> Econometrics and Free Software</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p><a rel="nofollow" href="https://youtu.be/I8vaCrVIR-Q?t=1h2m26s" target="_blank"><br>
<img title="This literally starts playing when you run both R and Python in the same session" src="https://i0.wp.com/www.brodrigues.co/blog/2018-12-30-reticulate/img/why%20not%20both.png?w=456" data-recalc-dims="1"></a></p>
</div>
<p>This short blog post illustrates how easy it is to use R and Python in the same R Notebook thanks to the<br>
<code>{reticulate}</code> package. For this to work, you might need to upgrade RStudio to the <a rel="nofollow" href="https://www.rstudio.com/products/rstudio/download/preview/" target="_blank">current preview version</a>.<br>
Let’s start by importing <code>{reticulate}</code>:</p>
<pre><code>library(reticulate)</code></pre>
<p><code>{reticulate}</code> is an RStudio package that provides “<em>a comprehensive set of tools for interoperability<br>
between Python and R</em>”. With it, it is possible to call Python and use Python libraries within<br>
an R session, or define Python chunks in R markdown. I think that using R Notebooks is the best way<br>
to work with Python and R; when you want to use Python, you simply use a Python chunk:</p>
<pre><code>```{python}
your python code here
```</code></pre>
<p>There’s even autocompletion for Python object methods:</p>
<p><img src="https://i2.wp.com/www.brodrigues.co/blog/2018-12-30-reticulate/img/autocompletion.png?w=456" data-recalc-dims="1"></p>
<p>Fantastic!</p>
<p>However, if you wish to use Python interactively within your R session, you must start the Python<br>
REPL with the <code>repl_python()</code> function, which starts a Python REPL. You can then do whatever you<br>
want, even access objects from your R session, and then when you exit the REPL, any object you<br>
created in Python remains accessible in R. I think that using Python this way is a bit more involved<br>
and would advise using R Notebooks if you need to use both languages.</p>
<p>I installed the Anaconda Python distribution to have Python on my system. To use it with <code>{reticulate}</code><br>
I must first use the <code>use_python()</code> function that allows me to set which version of Python I want<br>
to use:</p>
<pre><code># This is an R chunk
use_python(&quot;~/miniconda3/bin/python&quot;)</code></pre>
<p>I can now load a dataset, still using R:</p>
<pre><code># This is an R chunk
data(mtcars)
head(mtcars)</code></pre>
<pre><code>##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb
## Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
## Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
## Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
## Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
## Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
## Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1</code></pre>
<p>and now, to access the <code>mtcars</code> data frame, I simply use the <code>r</code> object:</p>
<pre><code># This is a Python chunk
print(r.mtcars.describe())</code></pre>
<pre><code>##              mpg        cyl        disp   ...            am       gear     carb
## count  32.000000  32.000000   32.000000   ...     32.000000  32.000000  32.0000
## mean   20.090625   6.187500  230.721875   ...      0.406250   3.687500   2.8125
## std     6.026948   1.785922  123.938694   ...      0.498991   0.737804   1.6152
## min    10.400000   4.000000   71.100000   ...      0.000000   3.000000   1.0000
## 25%    15.425000   4.000000  120.825000   ...      0.000000   3.000000   2.0000
## 50%    19.200000   6.000000  196.300000   ...      0.000000   4.000000   2.0000
## 75%    22.800000   8.000000  326.000000   ...      1.000000   4.000000   4.0000
## max    33.900000   8.000000  472.000000   ...      1.000000   5.000000   8.0000
## 
## [8 rows x 11 columns]</code></pre>
<p><code>.describe()</code> is a Python Pandas DataFrame method to get summary statistics of our data. This means that<br>
<code>mtcars</code> was automatically converted from a <code>tibble</code> object to a Pandas DataFrame! Let’s check its type:</p>
<pre><code># This is a Python chunk
print(type(r.mtcars))</code></pre>
<pre><code>## </code></pre>