Python engine (reticulate) for R Markdown for interoperability between Python and R chunks.


Step 0: Python - Set Directory. Load Data.


import os
import pandas

# Set/Get the current working directory
os.chdir("/Users/whinton/src/py/dds8555")
current_directory = os.getcwd()
print(f"Current working directory: {current_directory}")
## Current working directory: /Users/whinton/src/py/dds8555
flights = pandas.read_csv("flights.csv")
flights = flights[flights['dest'] == "ORD"]
flights = flights[['carrier', 'dep_delay', 'arr_delay']]
flights = flights.dropna()
print(flights.head())
##    carrier  dep_delay  arr_delay
## 5       UA       -4.0       12.0
## 9       AA       -2.0        8.0
## 25      MQ        8.0       32.0
## 38      AA       -1.0       14.0
## 57      AA       -4.0        4.0

Step 1: Python - Test Plot


import matplotlib.pyplot as plt
import numpy as np

t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2*np.pi*t)
plt.plot(t, s)

plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.grid(True)
plt.savefig("test.png")
plt.show()


Step 2: Calling Python from R


# Calling Python from R 

ggplot(py$flights, aes(carrier, arr_delay)) + geom_point() + geom_jitter()


Step 3: Calling R from Python


flights <- read.csv("flights.csv") %>%
  filter(dest == "ORD") %>%
  select(carrier, dep_delay, arr_delay) %>% 
  na.omit()
print(r.flights.head())
##   carrier  dep_delay  arr_delay
## 0      UA       -4.0       12.0
## 1      AA       -2.0        8.0
## 2      MQ        8.0       32.0
## 3      AA       -1.0       14.0
## 4      AA       -4.0        4.0

Step 4: Python - Use flights pre-defined in Step 0


print(flights.head())
##    carrier  dep_delay  arr_delay
## 5       UA       -4.0       12.0
## 9       AA       -2.0        8.0
## 25      MQ        8.0       32.0
## 38      AA       -1.0       14.0
## 57      AA       -4.0        4.0

References:

Tomasz Kalinowski, Kevin Ushey, JJ Allaire, RStudio, Yuan Tang. (2024) R Markdown Python Engine.
https://rstudio.github.io/reticulate/articles/r_markdown.html.


This study performed by Will Hinton