How to Seamlessly Integrate Python into R/RMarkdown Codes

Dima Diachkov
Data And Beyond
Published in
5 min readSep 24, 2023

Following my previous dive into integrating Python code into the RStudio environment, the next question that naturally arises is: can we make Python and R interact within the same script or document?

R and Python are both powerful languages, each with its unique strengths. While R excels at statistical analyses and data visualization, Python is versatile and has a vast ecosystem of libraries, particularly for machine learning. Merging their capabilities in a single document offers a unified workflow, reducing the need to switch between environments and tools.

Credits: Unsplash | Peter Neumann

Still stumbled over the question: “Why mix R and Python in the same script?”

Integrating Python and R in RStudio offers a unique advantage by allowing users to tap into the strengths of both languages. R is celebrated for its unparalleled statistical and data visualization capabilities, while Python shines in general programming and machine learning. Using both in a unified RStudio environment streamlines the workflow, eliminating the need to toggle between different platforms.

The reticulate package further enhances this experience by enabling seamless data exchange between R and Python. Moreover, the combined environment broadens the spectrum of accessible libraries and tools, ensuring that analysts can always choose the best tool for the task at hand. This amalgamation in RStudio signifies a move towards flexibility and efficiency in data analysis.

Reticulate in RMarkdown

Reticulate does more than let you run Python in RStudio. When used in RMarkdown, it permits Python chunks to exist alongside R chunks, enabling the two languages to communicate effortlessly.

Here’s how you can leverage this feature:

  1. Do the setup. In an RMarkdown document, you should first load the reticulate library.
## R chunk

```{r}
library(reticulate)
```

2. Add a Python Chunk: Once the reticulate library is loaded, you can create Python code chunks by using {python} as the chunk header. For instance:

## Python chunk

```{python}
def greet(name):
return f"Hello, {name}!"

greeting = greet("Medium audience")
print(greeting)
```

Now you have it, congrats! This is the compiled RMarkdown-based HTML page with R and Python codes.

Output for the code above

How to run data exchange between R and Python

This is where the magic truly lies. RMarkdown allows you to pass variables between R and Python seamlessly. A vital feature of reticulate in RMarkdown is its ability to transfer data between R and Python chunks.

Case 1. From R to Python

Here’s a demo with a simple R data frame:

## R chunk with input
```{r}
r_df <- data.frame(Name = c("Alice", "Bob"),
Age = c(30, 25))
```

Now, access this data frame in Python. Simply address it as a embedded of object r.

## Python chunk which READS input from R env

```{python}
python_df = r.r_df
print(python_df)
```
Output for the code above

Let’s knit our Rmarkdown file to see how the R input transferred to the Python output.

Output for the code above

See? It is so simple. Now you can collaborate with your Python colleagues to run joint research or you can benefit from implementing Python scripts using TensorFlow or scikit-learn. Isn’t that amazing? But sooner or later you need to transmit data back from python to the R environment. This is how you do it.

Case 2. From Python to R

Let’s create a Python list:

## Python chunk which READS input from R env

```{python}
python_df = r.r_df
print(python_df)
```

Let’s perform a common statistical task, for which R is suitable, like calculation of mean (I am kidding, but this is just a toy example).

This list can be retrieved in an R chunk:

## R chunk which READS input from Python env
```{r}
r_vector <- py$python_list
mean_value <- mean(r_vector)

print(mean_value)
```

The mean of the values in the Python list is mean_value. Just knit the file and you will see that the Python vector is fed to the R mean function, which returned 12.5.

Output for the code above

So, based on this flow of data between R and Python you can run very sophisticated codes. For example, you can parse website data with Python (here is my article on how to do it ) and then do your statistical analysis in R and present the result in RMarkdown format. Alternatively, you can plot data with ggplot2 in R (like I show here), but write after then put a piece, where you run a machine learning model with skikit-learn. Isn’t that a perfect combination?

However, be aware of:

  • Ensure that you have a consistent environment set up for both R and Python. Utilizing tools like virtual environments for Python can be beneficial.
  • Be aware of the dependencies. If you’re using a Python library or an R package, ensure it’s installed and loaded in the RMarkdown document.
  • Sometimes, the sequence of the chunks matters, especially when you’re passing data between R and Python chunks. Ensure that chunks are in the proper sequence.

Conclusion

The ability to combine R and Python in one cohesive RMarkdown document symbolizes the future of data analysis, where collaboration between tools and languages magnifies the potential of what we can achieve. Stay with me for more insights into the ever-evolving data science realm.

Please clap 👏 and subscribe if you want to support me. Thanks!❤️‍🔥

--

--

Dima Diachkov
Data And Beyond

Balancing passion with reason. In pursuit of better decision making in economic analysis and finance with data science via R+Python