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

Estimated time:

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="" 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=""></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=""></script> Below you’ll see the output: <img class="size-full wp-image-11682" src="" 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=""></script> Here’s what’s stored in the variable: <img class="size-full wp-image-11678" src="" 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=""></script> <img class="size-full wp-image-11686" src="" 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=""></script> Here’s the output from the above code snippet: <img class="size-full wp-image-11666" src="" 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=""></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=""></script> Here’s what the dataset looks like when displayed in Python: <img class="size-full wp-image-11674" src="" 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=""></script> <img class="size-full wp-image-11688" src="" 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="">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=""></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=""></script> Here’s the output displayed in the R console: <img class="size-full wp-image-11684" src="" 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=""></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=""> 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="" 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=""></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=""></script> <img class="size-full wp-image-11680" src="" 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=""></script> <img class="size-full wp-image-11676" src="" 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="" target="_blank" rel="noopener noreferrer">GitHub</a>, and print its first five rows: <script src=""></script> <img class="size-full wp-image-11668" src="" 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></code> method, as the figure won’t be displayed otherwise: <script src=""></script> <img class="size-full wp-image-11672" src="" 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="" 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="" target="_blank" rel="noopener noreferrer">@appsilon</a>. We’d love to see what you come up with. <blockquote>Want to crack your upcoming Python and Data Science coding interview? <a href="">Here are the top 7 questions you must know how to answer</a>.</blockquote>

Contact us!
Damian's Avatar
Damian Rodziewicz
Head of Sales
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Have questions or insights?
Engage with experts, share ideas and take your data journey to the next level!
Join Slack
Explore Possibilities

Take Your Business Further with Custom Data Solutions

Unlock the full potential of your enterprise with our data services, tailored to the unique needs of Fortune 500 companies. Elevate your strategy—connect with us today!

Estimate Project