Using reticulate's Python Engine with knitr

# load reticulate and set up engine
library(reticulate)
knitr::knit_engines$set(python = eng_python)

# use specific environment if available
# python <- "~/.virtualenvs/python-3.7.7-venv/bin/python"
# if (file.exists(python))
#   reticulate::use_python(python, required = TRUE)

# use rlang error handler
if (requireNamespace("rlang", quietly = TRUE))
  options(error = rlang::entrace)
reticulate::py_config()

Python variables live across chunk boundaries.

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

Plots generated by matplotlib are properly displayed.

```{python, fig.width=4, fig.height=3, dev="svg"} import matplotlib.pyplot as plt

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

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

```{python, fig.width=8, fig.height=3, dev="svg"}
import matplotlib.pyplot as plt

plt.plot(range(10))

```{python, fig.width=8, fig.height=3, dev="svg"} import matplotlib.pyplot as plt

plt.hist(range(10))

Python can access objects available in the R environment.

```r
x <- 1:5
y <- 6:10
print(r.x)
print(r['y'])

r.hello = "World"
r['answer'] = 42
print(hello)
print(answer)

Arbitrary R code can be evaluated from Python.

mpg = r["mtcars$mpg[1:5]"]
print(mpg)
y = "a,b,c".split(",")
print(y)

```{python echo=FALSE} print("Chunk with echo = FALSE")

- #130: The `results` chunk option is respected for Python outputs. Source, but not output, should show in the following output.

```{python results='hide'}
print ("Chunk with results = 'hide'")

```{python include=FALSE} print ("Chunk with include = FALSE")

- #130: The `eval` chunk option is respected for Python outputs.

```{python eval=FALSE}
# We have set 'eval = FALSE' here
r["abc"] = 1
exists("abc", envir = globalenv())

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

```{python, error=TRUE} raise RuntimeError("oops!") print("This line is still reached.")

Ensure that Exceptions are formatted w/ the type and notes.
```{python, error=TRUE}
class CustomException(Exception):
  pass

e = CustomException("oops!")

# BaseException.add_note() added in Python 3.11
import sys
if sys.version_info >= (3, 11):
  e.add_note("note 1: extra oopsy oops")
  e.add_note("note 2: in the future avoid oopsies")
else:
  print(sys.version_info)

raise e
print("This line is still reached.")

Ensure that lines with multiple statements are only run once.

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

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

```{python, eval=FALSE} here be syntax errors

Output from bare statements should also be printed.

```{python}
"Hello, world!"
[x for x in range(10)]

Expressions that generate plots should be shown, if that is the final statement within a chunk.

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

Ensure that outputs with results = "hold" are held to the end.

```{python, results="hold"} print(1) print(2) print(3)

plotly plots should be auto-printed and displayed.

```{python}
import plotly.express as px
fig = px.scatter(x=[1, 2, 3], y=[5, 4, 6])
fig

_repr_html_() methods are not called with unbound __self__ (https://github.com/rstudio/reticulate/issues/1249)

import pandas as pd
pt = pd.DataFrame()
type(pt)

_repr_markdown_() methods are supported (https://github.com/quarto-dev/quarto-cli/issues/1501):

from IPython.display import Markdown
from tabulate import tabulate
table = [["Sun",696000,1989100000],
         ["Earth",6371,5973.6],
         ["Moon",1737,73.5],
         ["Mars",3390,641.85]]
Markdown(tabulate(
  table, 
  headers=["Planet","R (km)", "mass (x 10^29 kg)"],
  tablefmt="pipe"
))

Setting option error=TRUE also allows parsing errors.

{python, error=TRUE} for i in range(3): print(i) print(i+10)



Try the reticulate package in your browser

Any scripts or data that you put into this service are public.

reticulate documentation built on Sept. 11, 2024, 8:31 p.m.