VOOZH about

URL: https://www.geeksforgeeks.org/r-language/integrating-python-code-with-r/

⇱ Integrating Python Code With R - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Integrating Python Code With R

Last Updated : 23 Jul, 2025

In data science and machine learning, both Python and R are popular programming languages. Python is known for its simplicity and variety of libraries, while R is mostly used in statistical analysis and data visualization. Sometimes, it helps to use both Python and R in the same project to take advantage of what each language does best. Integrating Python code with R is easy, making it simple for the two to work together.

Why Integrate Python with R?

It provides several advantages such as:

  • By combining Python and R, you can use Python's extensive libraries, such as TensorFlow and Pandas, alongside R's powerful statistical functions. This allows you to select the most effective tool for each task, improving efficiency and performance.
  • Integration reduces the need to switch between different tools or manually transfer data, creating a more cohesive workflow and minimizing errors.
  • Effective integration supports maintaining a single environment for both languages, simplifying code management and setup.
  • Teams with diverse programming preferences can work together more effectively. Integration allows team members to use their preferred language while contributing to a common project, enhancing collaboration.
  • Combining the strengths of Python and R provides a more comprehensive analytical approach. For example, you can leverage R for statistical analysis and Python for machine learning within the same project.
  • Integration tools facilitate the sharing of code, results, and insights between team members, promoting effective collaboration and knowledge exchange.

How to Integrate Python with R

There are two primary ways which are:

  1. Using the reticulate package in R
  2. Using Jupyter Notebooks with both R and Python kernels

Now we implement step by step those two methods in R programming Language.

Method 1: Using the reticulate Package in R

The {reticulate} package in R makes it easy to embed Python code within R Markdown documents, allowing us to take advantage of both languages in a single document.

Step 1: Install and Load the reticulate Package

First, install the reticulate package.

Step 2: Configure Python

Ensure that Python is properly configured.

Output:

python: C:/Python312/python.exe
libpython: C:/Python312/python312.dll
pythonhome: C:/Python312
version: 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)]
Architecture: 64bit
numpy: C:/Users/Tonmoy/AppData/Roaming/Python/Python312/site-packages/numpy
numpy_version: 1.26.4

NOTE: Python version was forced by PATH

python versions found:
C:/Python312/python.exe
C:/Users/Tonmoy/AppData/Local/Programs/Python/Python311/python.exe
C:/Users/Tonmoy/anaconda3/python.exe

Step 3: Run Python Code in R

Now, you can run Python code directly within your R script.

Output:

[1] 15

Step 4: Use Python Libraries in R

Now import and use Python libraries like numpy in R.

Output:

[1] 1 2 3 4

Step 5: Calling a Python Script

If there's a Python script named (script.py), it can be executed directly from R. In this example, we'll use a script that checks for Armstrong numbers, saved as am.py.

Output:

True
False

Method 2: Using Jupyter Notebooks with both R and Python kernels

Step 1: Install Jupyter Notebooks

If Jupyter is not installed, you can install it using Python.

pip install jupyter

Step 2: Install the R Kernel for Jupyter

To run R code in Jupyter, you need to install the R kernel.

Step 3: Create a New Jupyter Notebook

Open Jupyter by running the following command in your terminal or command prompt.

jupyter notebook

Step 4: Running Python and R Code Together

Now we will Run the Python and R Code Together.

Python Cell

Output:

15

R Cell

Output:

[1] 20

Step 5: Switching Between Python and R

Within the same notebook, we can switch between Python and R using the magic commands %%python and %%R.

Switching to R in a Python Cell:

Output:

[1] 20

Switching to Python in an R Cell:

Output:

10

Conclusion

Integrating Python code with R allows for the combination of the unique strengths of both languages, enhancing data analysis capabilities. Whether using the reticulate package in R or Jupyter Notebooks with both R and Python kernels, the process is straightforward and opens up a range of possibilities for projects. These tools enable the creation of more efficient, flexible, and collaborative workflows, leading to improved outcomes in data science tasks.

Comment
Article Tags:
Article Tags:

Explore