How to Use R and Python Together? Try These 2 Packages

Reading time:
time
min
By:
Dario Radečić
March 22, 2022

Data science is vastly different than programming. We use only four languages - R, Python, Julia, and SQL. Now, SQL is non-negotiable, as every data scientist must be proficient in it. Julia is still the new kid on the block. Many argue which is better - Python or R? But today, we ask a different question - how can you use R and Python together?

It might seem crazy at first, but hear us out. Both Python and R are stable languages used by many data scientists. Even seasoned package developers, such as <a href="https://www.kdnuggets.com/2015/12/using-python-r-together.html" target="_blank" rel="noopener noreferrer">Hadley Wickham</a>, borrow from <code>BeauftifulSoup</code> (Python) to make <code>Rvest</code> (R) web scraping packages. Reinventing the wheel makes no sense.

Today we’ll explore a couple of options you have if you want to use R and Python together in the same project. Let’s start with options for Python users.

Table of contents:
<ul><li><a href="#call-r-scripts">Calling R Scripts from Python</a></li><li><a href="#run-r-code">Running R Code from Python with rpy2</a></li><li><a href="#call-python-scripts">Calling Python Scripts from R</a></li><li><a href="#run-python-code">Running Python Code from R with R Markdown</a></li><li><a href="#conclusion">Conclusion</a></li></ul>

<hr />

<h2 id="call-r-scripts">How to Call R Scripts from Python</h2>
Using R and Python together at the same time is incredibly easy if you already have your R scripts prepared. Calling them from Python boils down to a single line of code. Let’s cover the R script before diving further.

It’s really a simple one, as it only prints some dummy text to the console:

<script src="https://gist.github.com/darioappsilon/661be37af7e939401741d7015427c201.js"></script>

On the Python end, you’ll need to use the <code>subprocess</code> module to run a shell command. All R scripts can be run with the <code>Rscript &lt;script-path&gt;</code> call:

<script src="https://gist.github.com/darioappsilon/47c8b7d674f543ee4895f862193a3965.js"></script>

Below you’ll see the output:

<img class="size-full wp-image-11682" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e84feabe8f16c323238_running-an-r-script-from-python.webp" alt="Image 1 - Running an R script from Python" width="558" height="122" /> Image 1 - Running an R script from Python

The line was successfully printed to the console, and a zero was returned. That’s the thing - this approach is excellent if your R script performs tasks one after the other. It falls short if you want to use the output from R code in Python.

It’s a shortcoming that the next option for using R and Python together addresses.
<h2 id="run-r-code">How to Run R Code from Python with rpy2</h2>
Now we’ll dive into the good stuff. You’ll have to install the <code>rpy2</code> package in Python to follow along. It’s assumed you also have R installed and configured.

To start, we’ll use the <code>robjects</code> submodule to access R objects, such as the number PI:

<script src="https://gist.github.com/darioappsilon/60835f3de6bede8807ea097db8ee39b6.js"></script>

Here’s what’s stored in the variable:

<img class="size-full wp-image-11678" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e86fdb8ad62e9cd9f71_PI-R-Object.webp" alt="Image 2 - PI R object" width="488" height="140" /> Image 2 - PI R object

You can check its type. It's an R-specific float vector:

<script src="https://gist.github.com/darioappsilon/d7b0bc7090740acf1be6b4f6a800e909.js"></script>

<img class="size-full wp-image-11686" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e87790b9e1ad88e1580_type-of-the-R-PI-object.webp" alt="Image 3 - Type of the R PI object" width="694" height="94" /> Image 3 - Type of the R PI object

There’s a lot more you can do than access individual R objects. For example, you can also declare and run R functions. The code snippet below shows you how to declare a function for adding numbers and call it two times. Just to be extra careful, make sure to surround the R code with triple quotation marks:

<script src="https://gist.github.com/darioappsilon/735c34feb21a51f6eb210e697f40bb90.js"></script>

Here’s the output from the above code snippet:

<img class="size-full wp-image-11666" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e887a92f47f699ab331_calling-R-function-in-python.webp" alt="Image 4 - Calling R function in Python" width="504" height="226" /> Image 4 - Calling R function in Python

Many times you won’t find the built-in R packages enough for your specific use case. You can install additional, external R packages through Python with the <code>rpackages</code> submodule:

<script src="https://gist.github.com/darioappsilon/8e303c7177584d844888be3ee47df57d.js"></script>
<h3>Dataframes</h3>
There’s also an option to work with R dataframes in Python. The code snippet below shows you how to import the <code>datasets</code> subpackage and access the well-known MTcars dataset:

<script src="https://gist.github.com/darioappsilon/cc6019c9ac2777096ab3200c4da6b3ed.js"></script>

Here’s what the dataset looks like when displayed in Python:

<img class="size-full wp-image-11674" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e89790b9e1ad88e175a_MTcars-dataset-as-a-data-frame.webp" alt="Image 5 - MTcars dataset as a data frame" width="1148" height="724" /> Image 5 - MTcars dataset as a data frame
<h3>Visualization</h3>
And for the last bit, we’ll show you how to visualize the dataset with R’s <code>ggplot2</code> package. As of now, you can’t display the figures directly in the notebook, so you’ll need to save the figure to a file using the <code>grDevices</code> package. The code responsible for plotting should go between the call to <code>grdevices.png()</code> and <code>grdevices.dev_off()</code>, so keep that in mind for future reference:

<script src="https://gist.github.com/darioappsilon/e2ffe80e05a70504a87fb48aa77bfb3a.js"></script>

<img class="size-full wp-image-11688" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e8a44295a0f332395d2_using-ggplot2-in-python.webp" alt="Image 6 - Using ggplot2 in Python" width="1024" height="512" /> Image 6 - Using ggplot2 in Python

And that’s how you can use R and Python together at the same time by running R code from Python. Let’s reverse the roles next and explore options for R users.
<blockquote>Looking to style your scatter plots? <a href="https://appsilon.com/ggplot-scatter-plots/">Read our comprehensive guide to stunning scatter plots with R and ggplot2</a>.</blockquote>
<h2 id="call-python-scripts">How to Call Python Scripts from R</h2>
R users have an even easier time running scripts from the opposite programming language. You’ll have to install the <code>reticulate</code> package if you want to follow along, as it’s responsible for running Python scripts and configuring Python environments.

First things first, let’s write a Python script. It will be a simple one, as it prints a single line to the console:

<script src="https://gist.github.com/darioappsilon/329ef8f5f94a85c4f886dc3c9ec07a3c.js"></script>

In R, you’ll have to import the <code>reticulate</code> package and call the <code>py_run_file()</code> function with a path to the Python script provided:

<script src="https://gist.github.com/darioappsilon/6d7c69eb572026151890f83e4cb432bb.js"></script>

Here’s the output displayed in the R console:

<img class="size-full wp-image-11684" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e8b790b9e1ad88e18c3_running-python-scripts-from-R.webp" alt="Image 7 - Running Python scripts from R" width="1102" height="106" /> Image 7 - Running Python scripts from R

As you can see, everything works as advertised. You can go one step further and use a specific Python version, virtual environment, or Anaconda environment. Use any of the three function calls below as a reference:

<script src="https://gist.github.com/darioappsilon/804eb8ce07380caad4d632a35238d2a1.js"></script>

Next, we’ll explore more advanced ways R users can use R and Python at the same time.
<blockquote><strong>Can R programmers make Machine Learning models? Yes! Learn how with <a href="https://appsilon.com/fast-ai-in-r/">fast.ai in R</a>. </strong></blockquote>
<h2 id="run-python-code">How to Run Python Code from R</h2>
The <code>reticulate</code> package comes with a Python engine you can use in R Markdown. Reticulate <a href="https://www.rstudio.com/blog/reticulate-r-interface-to-python/" target="_blank" rel="noopener noreferrer">allows you to</a> run chunks of Python code, print Python output, access Python objects, and so on.

To start, create a new R Markdown (Rmd) file and do the usual setup - library imports and Python location configuration:

<script src="https://gist.github.com/darioappsilon/f77f5a2fd123152f93fc45833c684a49.js"></script>

You can now create either an R or a Python block by writing three backticks and specifying the language inside of curly brackets. We’ll start with Python. The code snippet below imports the Numpy library, declares an array, and prints it:

<script src="https://gist.github.com/darioappsilon/e60a454f890a367c821a3098f19f2aea.js"></script>

<img class="size-full wp-image-11680" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e8c30ca449bf54ba80f_python-list-printed-in-r-markdown.webp" alt="Image 8 - Python list printed in R Markdown" width="342" height="124" /> Image 8 - Python list printed in R Markdown

But what if you want to convert Python’s Numpy array to an R vector? As it turns out, you can access Python objects in R by prefixing the variable name with <code>py$</code>. Here’s an example:

<script src="https://gist.github.com/darioappsilon/afb115d9aba564a4538dd973aa985e4f.js"></script>

<img class="size-full wp-image-11676" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e8e6bdc8f59ff86647f_numpy-array-converted-to-an-r-vector.webp" alt="Image 9 - Numpy array converted to R vector" width="350" height="104" /> Image 9 - Numpy array converted to R vector

As you would imagine, the possibilities from here are endless. We’ll now show you how to import the Pandas library, load a dataset from <a href="https://gist.githubusercontent.com/seankross/a412dfbd88b3db70b74b/raw/5f23f993cd87c283ce766e7ac6b329ee7cc2e1d1/mtcars.csv" target="_blank" rel="noopener noreferrer">GitHub</a>, and print its first five rows:

<script src="https://gist.github.com/darioappsilon/0612d1ec128e47e59bf8995fe3403323.js"></script>

<img class="size-full wp-image-11668" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e90db456a59ff5ce634_head-of-the-MTcars-dataset.webp" alt="Image 10 - Head of the MTcars dataset" width="1670" height="470" /> Image 10 - Head of the MTcars dataset

Easy, right? You can import any Python library and write any Python code you want, and then access the variables and functions declared with R.

Python’s de-facto standard data visualization library is Matplotlib, and it’s also easy to use in R Markdown. Just remember to call the <code>plt.show()</code> method, as the figure won’t be displayed otherwise:

<script src="https://gist.github.com/darioappsilon/0b43d90c187c2c9426548ea2597a3d0f.js"></script>

<img class="size-full wp-image-11672" src="https://webflow-prod-assets.s3.amazonaws.com/6525256482c9e9a06c7a9d3c%2F65b01e9048b83fcd2e556b4b_Matplotlib-chart-in-r-markdown.webp" alt="Image 11 - Matplotlib chart in R Markdown" width="1942" height="1194" /> Image 11 - Matplotlib chart in R Markdown

And that’s how you can run Python code in R and R Markdown. That’s all we wanted to cover in today’s article, so let’s make a brief summary next.

<hr />

<h2 id="conclusion">Summary of Using R and Python Together</h2>
Today you’ve learned how to use R and Python together from the perspectives of both R and Python users. Hopefully, you can now combine the two languages to get the best of both worlds. For example, some R packages, such as <code>autoarima</code> have no direct competitor in Python. Reinventing the wheel doesn’t make sense. So don't. Just preprocess the data with Python and model it with R.

Why don’t you give it a try as a homework assignment? Download the <a href="https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv" target="_blank" rel="noopener noreferrer">Airline passengers</a> dataset, load and preprocess it in Python, and R’s <code>autoarima</code> package to make the forecasts. Share your results with us on Twitter – <a href="https://twitter.com/appsilon" target="_blank" rel="noopener noreferrer">@appsilon</a>. We’d love to see what you come up with. Check out our latest resources - ebooks, cheatsheets and more.
<blockquote>Want to crack your upcoming Python and Data Science coding interview? <a href="https://appsilon.com/data-science-coding-interview-questions/">Here are the top 7 questions you must know how to answer</a>.</blockquote>

Have questions or insights?

Engage with experts, share ideas and take your data journey to the next level!

Is Your Software GxP Compliant?

Download a checklist designed for clinical managers in data departments to make sure that software meets requirements for FDA and EMA submissions.
Explore Possibilities

Share Your Data Goals with Us

From advanced analytics to platform development and pharma consulting, we craft solutions tailored to your needs.

Talk to our Experts
r
python
tutorials