How do I use R while my collaborators use Python?
In earlier posts, we have seen the advantages of using R for data analysis and research purposes (here). The R language is a lucrative tool for researchers, data scientists especially in new methodologies and visualization techniques. I have seen many researchers used to move between R and Python several times, due to several reasons. One of the major reasons is that if their collaborators have been using Python in their team. One may feel that the code integration in the same language can be an easy task and it can be challenging to integrate the scripts from two different languages (i.e. R and Python).
I am sharing my own experience, how I prefer the R language in my research activities, even when my collaborators were working in Python, and how we integrated different scripts to have fruitful results.
My team was working on modern power systems in Europe with the PyPSA toolbox. PyPSA is a free software toolbox for simulating and optimizing modern power systems. This toolbox is designed in/for Python language and uses several optimization tools to achieve accurate simulations of modern power systems.
As a researcher, my task was to integrate this simulation with some of our data science techniques and methodologies. One may feel that doing all those activities in Python can be convenient for the future integrations of the research codes. However, I observed the integration within R and Python is buttery smooth, nowadays, thanks to the ‘reticulate’ package.
This package gives me an opportunity to import the Python functions or scripts in the R environment with input arguments. Also, it allows us to change the R input arguments into the Python equivalent format, as well as, convert the Python output into the R equivalent data formats.
Using the ‘reticulate’ package is a very simple process, provided all required settings and installations (Python and packages) are done in advance. Initially, one may find it frustrating, if he/she is experiencing Python for the first time, but the process gets handy once everything starts working. For smooth operations, I would like to host a new post describing how to integrate R and Python step-by-step with the ‘reticulate’ package with a description of all required settings. I will link that post here, in the near future. However, for the time being, some simple and brief steps are discussed below:
Step 2: Make sure that your PC has already installed the Python language. It is recommended to access the Python script through Anaconda.
Step 3: Keep the Python script (.py) file in the R project folder. For example, an R project folder is shown below:
Step 4: Import the Python scripts with py_run_file() or source_python() functions as shown below:
The functions such as r_to_py() and py_to_r() are the argument interfaces to convert R to Python and Python to R equivalent data formats, respectively. These supportive functions make accessing Python scripts easy in the R environment.
This integration allows R users to use all CRAN advantages such as R package, visualizations, RMarkdown, and Shiny web app development with the imported Python scripts.
This way, the ‘reticulate’ package allowed me to be an R user even when my team co-mates were working in Python.
Dr. Neeraj Dhanraj Bokde,
Aarhus University, Denmark