Setup

library(reticulate)
readr::write_csv(nycflights13::flights, file = "flights.csv")

print(reticulate::py_config())
python:         /opt/rstudio-connect/mnt/python-environments/ghcr.io_rstudio_content-pro__r4.1.0-py3.8.8-bionic/pip/3.8.8/x8MsyAVKiG31Nk12SDjbTw/bin/python
libpython:      /opt/python/3.8.8/lib/libpython3.8.so
pythonhome:     /opt/rstudio-connect/mnt/python-environments/ghcr.io_rstudio_content-pro__r4.1.0-py3.8.8-bionic/pip/3.8.8/x8MsyAVKiG31Nk12SDjbTw:/opt/rstudio-connect/mnt/python-environments/ghcr.io_rstudio_content-pro__r4.1.0-py3.8.8-bionic/pip/3.8.8/x8MsyAVKiG31Nk12SDjbTw
version:        3.8.8 (default, Apr 13 2021, 19:58:26)  [GCC 7.3.0]
numpy:          /opt/rstudio-connect/mnt/python-environments/ghcr.io_rstudio_content-pro__r4.1.0-py3.8.8-bionic/pip/3.8.8/x8MsyAVKiG31Nk12SDjbTw/lib/python3.8/site-packages/numpy
numpy_version:  1.22.0

NOTE: Python version was forced by RETICULATE_PYTHON

From Python to R

Use Python to manipulate data

import pandas
flights = pandas.read_csv("flights.csv")
flights = flights[flights['dest'] == "ORD"]
flights = flights[['carrier', 'dep_delay', 'arr_delay']]
flights = flights.dropna()
flights
       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
...        ...        ...        ...
336645      AA      -12.0      -37.0
336669      UA       -7.0      -13.0
336675      MQ       -7.0      -11.0
336696      B6       -5.0      -23.0
336709      AA      -13.0      -38.0

[16566 rows x 3 columns]

Use R to visualize Pandas DataFrame

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


From R to Python

Use R to read and manipulate data

library(readr)
library(dplyr)
flights <- read_csv("flights.csv") %>%
  filter(dest == "ORD") %>%
  select(carrier, dep_delay, arr_delay) %>%
  na.omit()

Use Python to print R dataframe

print(r.flights.head(10))
  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
5      UA        9.0       20.0
6      UA        2.0       21.0
7      AA       -6.0      -12.0
8      MQ       39.0       49.0
9      B6       -2.0       15.0

See the reticulate website for details.

LS0tCnRpdGxlOiAiVGhlIHJldGljdWxhdGUgcGFja2FnZSIKc3VidGl0bGU6ICJJbnRlZ3JhdGlvbiBvZiBQeXRob24gaW50byBSIHByb2plY3RzIgpvdXRwdXQ6IGh0bWxfbm90ZWJvb2sKLS0tCgoqKioKCiMjIyBTZXR1cAoKYGBge3Igc2V0dXB9CmxpYnJhcnkocmV0aWN1bGF0ZSkKcmVhZHI6OndyaXRlX2NzdihueWNmbGlnaHRzMTM6OmZsaWdodHMsIGZpbGUgPSAiZmxpZ2h0cy5jc3YiKQoKcHJpbnQocmV0aWN1bGF0ZTo6cHlfY29uZmlnKCkpCmBgYAoKKioqCgojIEZyb20gUHl0aG9uIHRvIFIKCgojIyMgVXNlIFB5dGhvbiB0byBtYW5pcHVsYXRlIGRhdGEKCmBgYHtweXRob24gZGF0YX0KaW1wb3J0IHBhbmRhcwpmbGlnaHRzID0gcGFuZGFzLnJlYWRfY3N2KCJmbGlnaHRzLmNzdiIpCmZsaWdodHMgPSBmbGlnaHRzW2ZsaWdodHNbJ2Rlc3QnXSA9PSAiT1JEIl0KZmxpZ2h0cyA9IGZsaWdodHNbWydjYXJyaWVyJywgJ2RlcF9kZWxheScsICdhcnJfZGVsYXknXV0KZmxpZ2h0cyA9IGZsaWdodHMuZHJvcG5hKCkKZmxpZ2h0cwpgYGAKCiMjIyBVc2UgUiB0byB2aXN1YWxpemUgUGFuZGFzIERhdGFGcmFtZQoKYGBge3IsIGZpZy53aWR0aD03LCBmaWcuaGVpZ2h0PTN9CmxpYnJhcnkoZ2dwbG90MikKZ2dwbG90KHB5JGZsaWdodHMsIGFlcyhjYXJyaWVyLCBhcnJfZG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