DEV Community

Shalini Battoo
Shalini Battoo

Posted on

Calling Python from R

There is a constant debate regarding the best tool for data science and machine learning between R and Python. I feel both of them have their pros and cons and it really depends on the task at hand, the requirement from a client in case of a project or just personal likeness for a developer/coder.

We will not get into a debate about the best tool in the market. Rather what I want to discuss is about using both of them together.
Why use one, when we can get Best of Both the worlds.

The library used in R to connect to Python is reticulate.
In case you have never worked with reticulate earlier, you will not have in the package library of R. It needs to be installed. This can be done by writing
install.packages(reticulate)
The library can be loaded by using the function given below once it is successfully installed.
library(reticulate)

In order to run any script of python from R we will use the functions source_python() and py_run_file().

The first function makes the objects created available in the R environment. The second function creates a py object which allows to access the other objects created by calling the python script. It creates a dictionary (type of a variable in python) associated with code execution.

For example if we have a script abc.py, then this can be run by using the function source_python('abc.py'). In case the script consists of some function xyz(p,q), where p and q are the arguments being passed to the function xyz. After the script is sourced this function can be directly called in R.

This package is very powerful and has other functions for making the integration between Python and R easier. In case you want to know about the other functions available in the library, refer to the Link
I hope this proves helpful in case you are trying to call Python scripts from R and take advantage of the strengths of Python from within R.

Top comments (0)