Python + R no mesmo documento

Python

Code
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
Code
df = pd.DataFrame({
    'nome': ['Ana', 'Bruno', 'Carla'],
    'idade': [25, 30, 28],
    'cidade': ['SP', 'RJ', 'BH']
})
NameError: name 'pd' is not defined
Code
print(df)
NameError: name 'df' is not defined

R

Code
library(ggplot2)

# Cria dados
dados <- data.frame(
  x = 1:10,
  y = rnorm(10)
)

# Plot simples
ggplot(dados, aes(x, y)) +
  geom_point(color = "steelblue", size = 3) +
  geom_line(alpha = 0.5) +
  theme_minimal() +
  labs(title = "Gráfico em R")

Compartilhando dados (R acessando Python)

Code
library(reticulate)

# Acessa o DataFrame criado no Python
py$df
Error in `py_get_attr()`:
! AttributeError: module '__main__' has no attribute 'df'
Run `reticulate::py_last_error()` for details.

Compartilhando dados (Python acessando R)

Code
# Acessa o dataframe criado no R
print(r.dados)
{'x': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'y': [2.1187624592905645, -1.2619788580018043, 3.490549575621708, 0.7747433159195019, -1.6937387859539976, -0.8152919467496564, -0.2052291016659727, 0.21838414313377683, 0.9845939501293313, -0.5663584084861358]}