Author

D K. Muriithi | CDAM-Chuka University

Published

May 28, 2026

Use Python Chunks in R Markdown

1. Introduction

R Markdown allows you to combine R, Python, text, tables, and visualizations in a single reproducible document. By using the reticulate package, you can execute Python code directly inside an R Markdown (.Rmd) file.

This approach is useful when combining:

  • R for statistical analysis and visualization
  • Python for machine learning, AI, and data processing

For example, a data scientist can use R for EDA and visualization and Python for machine learning models within the same report.


2. Prerequisites

Before running Python code in R Markdown, ensure:

Install Required Packages in R

#install.packages("reticulate")
#install.packages("rmarkdown")

Load the package:

library(reticulate)
library(rmarkdown)

3. Verify Python Installation

R Markdown uses Python installed on your machine.

Check Python configuration:

library(reticulate)

#py_config()

If Python is not detected, specify the path manually:

Windows Example

use_python("C:/Users/Admin/Documents/.virtualenvs/r-reticulate/Scripts/python.exe")

4. Structure of an R Markdown Document

An R Markdown document contains:

  1. YAML header
  2. Text/Markdown
  3. Code chunks (R or Python)

6. Working with Sample Data

You can create datasets directly in Python chunks.

Example: Generate Sample Data

import pandas as pd
import numpy as np

np.random.seed(123)

data = pd.DataFrame({
    'Age': np.random.randint(20, 60, 10),
    'Income': np.random.randint(30000, 100000, 10),
    'Malaria_Status': np.random.choice([0,1], 10)
})

print(data.head())
   Age  Income  Malaria_Status
0   22   63710               1
1   48   95647               0
2   54   47747               1
3   58   65662               1
4   37   98861               1

7. Data Visualization in Python Chunks

Python visualization libraries work directly inside R Markdown.

Example: Histogram

import matplotlib.pyplot as plt

plt.hist(data['Age'])
plt.title("Age Distribution")
plt.xlabel("Age")
plt.ylabel("Frequency")
plt.show()


8. Using Seaborn for Visualization

Example using seaborn:

import seaborn as sns
import matplotlib.pyplot as plt

sns.countplot(data=data, x='Malaria_Status')
plt.title("Malaria Status Distribution")
plt.show()


9. Using Built-in Datasets

Example with the diamonds dataset:

import seaborn as sns
import matplotlib.pyplot as plt

diamonds = sns.load_dataset("diamonds")
diamonds.head()
   carat      cut color clarity  depth  table  price     x     y     z
0   0.23    Ideal     E     SI2   61.5   55.0    326  3.95  3.98  2.43
1   0.21  Premium     E     SI1   59.8   61.0    326  3.89  3.84  2.31
2   0.23     Good     E     VS1   56.9   65.0    327  4.05  4.07  2.31
3   0.29  Premium     I     VS2   62.4   58.0    334  4.20  4.23  2.63
4   0.31     Good     J     SI2   63.3   58.0    335  4.34  4.35  2.75
sns.countplot(data=diamonds, x="cut")
plt.xticks(rotation=45)
([0, 1, 2, 3, 4], [Text(0, 0, 'Ideal'), Text(1, 0, 'Premium'), Text(2, 0, 'Very Good'), Text(3, 0, 'Good'), Text(4, 0, 'Fair')])
plt.show()

Equivalent R code:

library(ggplot2)
ggplot(diamonds, aes(x = cut)) +
  geom_bar()


10. Passing Data Between R and Python

One major advantage of R Markdown is sharing data between R and Python.

R to Python

R chunk:

x <- c(1,2,3,4,5)
x
[1] 1 2 3 4 5

Python chunk:

print(r.x)
[1.0, 2.0, 3.0, 4.0, 5.0]

Python to R

Python chunk:

y = [10,20,30]
y
[10, 20, 30]

R chunk:

py$y
[1] 10 20 30

11. Chunk Options

Chunk options control execution and output.

Example:

print("Show code and output")
Show code and output

Common options:

Option Meaning
echo=TRUE Show code
echo=FALSE Hide code
warning=FALSE Hide warnings
message=FALSE Hide messages
eval=FALSE Show code but do not run

Example:

import warnings
warnings.filterwarnings("ignore")

12. Advantages of Python Chunks in R Markdown

  1. Combine R and Python in one document
  2. Reproducible analysis
  3. Easy reporting
  4. Supports machine learning workflows
  5. Ideal for research and academic reports
  6. Integrates statistical modeling and AI

13. Summary

Python chunks in R Markdown allow users to execute Python code directly within .Rmd documents. Through the reticulate package, users can integrate statistical analysis in R with machine learning and AI tools in Python, making it a powerful environment for reproducible research, analytics, and reporting.

Documentation:

R Markdown documentation

reticulate documentation