Closed Captioning Closed captioning available on our YouTube channel

Run Python in R code

InfoWorld | Feb 15, 2019

While R is a useful language, Python is also great for data science and general-purpose computing. See how to run Python code within an R script and pass data between Python and R

Copyright © 2019 IDG Communications, Inc.

Hi. I’m Sharon Machlis at IDG Communications, here with Do More With R: Run Python in your R code.
As much as I love R, it’s clear that Python is also a great language – both for data science and general-purpose computing. And there can be good reasons an R user would want to do some things in Python. Maybe it’s a great library that doesn’t have an R equivalent (yet). Or an API you want to access that has sample code in Python but not R.
Thanks to the R reticulate package, you can run Python code right within an R script. And pass data back and forth between Python and R.
So how can you do this? Besides reticulate, you need Python installed on your system. You’ll also need any Python modules, packages, and files your Python code depends on.
Let’s load reticulate and see how this works.
I don’t know all that much Python, so I’ll keep this simple. I’ll start with two lines of Python code I’d like to run: import the NumPy package for basic scientific computing, and create an array of 4 numbers.
Here’s one way to do that right in your R script
The py_run_string() function executes whatever Python code is within the parentheses and quotation marks. I’ll run those two lines of Python code within R commands [run those 2 lines of code].
It looks like nothing happened. If I run print(my_python_array) in R, nothing happens.
But look what happens if I run Python’s print command inside the py_run_string function
That clearly isn’t R code! Python is running inside my R script.
It’s going to get annoying running Python code line by line like this, though, if you have more than a couple of lines of code. There are a few other ways to run Python in R and reticulate. One is to put all the Python code in a regular .py file, and use the py_run_file() function. But the way I like is to use an R Markdown document.
R Markdown lets you combine text, code, code results, and visualizations in a single document. Code chunks start with 3 backticks and end with 3 backticks, and they have a grey background by default in RStudio. This first chunk is for R code – you can see that with the r after the opening bracket. I’m loading the reticulate package and then specifying the version of Python I want to use. (If you don’t specify, it’ll use your system default.)
This second chunk is for Python code, and I just type the Python like I would in a Python file. I import numpy, create an array, and print the array.
Now here’s the really cool part. I can use that array in R by referring to it as py, dollar sign, and the name of the Python variable. In this next code chunk, I’m storing that Python array in an R variable called my_r_array. And then I check the class of that array.
It’s still an “array,” which isn’t exactly what you’d expect for an R object. But I can turn it into a regular vector with as.vector(), and run whatever R operations I’d like on it. Like multiplying each item by 2. You can see that the operation worked.
Next cool part: I can use that R variable back in Python, by using r period and the name of the R variable. I’ll store r dot my_r_vector in a new Python variable. If I run this code, you’ll see that worked, too.
Just to show you something a bit more useful, I’ve got the sample YouTube Python script to search for videos by topicI customized this template to add my YouTube API key and search for videos about the R testthat package. Hey, my Do More With R video is the first one in the results!
That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at https go dot infoworld dot com slash more with R, all lowercase except for the R. You can also find the Do More With R playlist on YouTube. Hope to see you next episode!
Featured videos from