Python variables live across chunk boundaries.

x = 1
y = 2
print(x)
## 1
print(y)
## 2

Plots generated by matplotlib are properly displayed.

import matplotlib.pyplot as plt

plt.plot([1, 2, 3, 4])
plt.show()

plt.plot([1, 2, 3, 4])
plt.show()

Python can access objects available in the R environment.

x <- 1:5
y <- 6:10
print (r.x)
## [1, 2, 3, 4, 5]
print (r['y'])
## [6, 7, 8, 9, 10]
r.hello = "World"
r['answer'] = 42
print(hello)
## [1] "World"
print(answer)
## [1] 42

Arbitrary R code can be evaluated from Python.

mpg = r["mtcars$mpg[1:5]"]
print(mpg)
## [21.0, 21.0, 22.8, 21.4, 18.7]
y = "a,b,c".split(",")
print(y)
## ['a', 'b', 'c']
## Chunk with echo = FALSE
print ("Chunk with results = 'hide'")
# We have set 'eval = FALSE' here
r["abc"] = 1
exists("abc", envir = globalenv())
## [1] FALSE

Respect the error=TRUE chunk option – allow execution even after a Python error occurs.

raise RuntimeError("oops!")
## Error in py_call_impl(callable, dots$args, dots$keywords): RuntimeError: oops!
## 
## Detailed traceback: 
##   File "<string>", line 1, in <module>
print("This line is still reached.")
## This line is still reached.

Ensure that lines with multiple statements are only run once.

print("abc"); print("123")
## abc
## 123

Ensure that syntax errors do not block document generation when eval = FALSE.

here be syntax errors

Output from bare statements should also be printed.

"Hello, world!"
## 'Hello, world!'
[x for x in range(10)]
## [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Expressions that generate plots should be shown.

import numpy as np, pandas as pd
a = np.random.normal(size=1000)
pd.Series(a).hist()