library(reticulate) knitr::knit_engines$set(python = eng_python)
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 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)
echo
chunk option is respected (for TRUE
and FALSE
values). Output, but not source, should show in the following output.```{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'")
include
chunk option is respected for Python outputs. No chunk output should appear following this bullet.```{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 lines with multiple statements are only run once. ```{python} print("abc"); print("123")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.