Five ways to work seamlessly between R and Python in the same project | by Keith McNulty | Towards Data Science

Lately I’ve found myself doing more projects using both R and Python together. It has become more important to me to use the best tools for the job and not be constrained by a single language. There are things that Python does best and there are things that R does best, so if we can use both when we need to, we can produce our best. In a recent example I wanted to create a Shiny app to produce parameterized Powerpoint documents, and wanted to code my data processing in R to take advantage of the tidyverse but used Python to code the Powerpoint editing which was easy in the python-pptx package.
It’s now really easy to work in both languages in the RStudio IDE. For this to work you’ll need to have the reticulate package installed and loaded in R. Here are five things you can do that allow you to have a seamless experience coding in both languages in the same project:
To avoid any issues related to using the wrong Python interpreter, you can define your Python environment on project startup by creating a .Rprofile file and saving it in the project directory. Your .Rprofile contains code that will be executed whenever your project starts up. I usually have these two lines of code in my .Rprofile :
Sys.setenv(RETICULATE_PYTHON = "<path>")print(paste("Python environment forced to", Sys.getenv("RETICULATE_PYTHON")))
where <path> is the path to the Python executable in the Python environment you wish to use. The first command sets this as your python executable. The second command prints a confirmation of this which will display in your terminal every time you start your project. Here’s what my terminal looks like when I start one of my projects:
You’ll need to test your Python code as well as your R code as you build your project. The repl_python() function in the reticulate package switches your R terminal to a Python terminal so you can code live in Python. You can exit this using the exit command, then code some more in R, then if you go back into your Python terminal it will still remember all the objects from the last session, meaning its easy to seamlessly move between the languages. Here’s a simple example:
Any fairly standard data objects like values, lists and dataframes can be exchanged between Python and R. To use an R object called my_r_object in Python, you you call it using r.my_r_object. To use a Python object called my_python_objectin R, you can call it using py$my_python_object. Here’s another example of this in action where I bring themtcars data set from R into Python:
Data objects will be translated into their equivalent structures in the different languages. For example, a list in R will be translated into a dict in Python:
When you are using these two languages together, you’ll eventually need to execute Python code as a function in R. If you write your Python function into a file and then source that file in R using source_python() , your Python function becomes an R function for you to use as you need to. Here is a simple example where I write a Python function to add two numbers together and save it in the file sumof.py:
def sumof(a, b):  return a+b
Now I can source sumof.py and it becomes and R function:
With your .Rprofile appropriately set, you can create documents that have code chunks in the two languages and you can exchange objects between the code chunks. Here’s some simple R Markdown code which demonstrates this:
This produces the following output:
In the same way, you can write functions in one language and easily use them in another:
For me this functionality has made so many more things possible in my work. I hope these tips help you get started in exploring the best of what both Python and R have to offer.
Originally I was a Pure Mathematician, then I became a Psychometrician and a Data Scientist. I am passionate about applying the rigor of all those disciplines to complex people questions. I’m also a coding geek and a massive fan of Japanese RPGs. Find me on LinkedIn or on Twitter. Also check out my blog on drkeithmcnulty.com.