To add a Python code chunk to an R Markdown document, you can use the chunk header , e.g.,

    The Python support in R Markdown and knitr is based on the reticulate package (), and one important feature of this package is that it allows two-way communication between Python and R. For example, you may access or create Python variables from the R session via the object py in reticulate:

    1. ```{r, setup}
    2. library(reticulate)
    3. ```
    4. Create a variable `x` in the Python session:
    5. x = [1, 2, 3]
    6. Access the Python variable `x` in an R code chunk:
    7. ```{r}
    8. py$x
    9. ```
    10. and pass a data frame to `y`:
    11. ```{r}
    12. py$y <- head(cars)
    13. ```
    14. Print the variable `y` in Python:
    15. ```{python}
    16. ```

    Ushey, Kevin, JJ Allaire, and Yuan Tang. 2020. Reticulate: Interface to Python. https://github.com/rstudio/reticulate.