Search code examples
pythonrr-markdownknitrreticulate

Is "library(reticulate)" needed in knitr setup chunk


If I don't include the "library(reticulate)" line I cannot access the "py" object inside R chunks, but I can always access "r" object from within python chunks with or without including the line. So, is it really needed or this is a buggy behavior? I'm knitting with pdflatex.


Solution

  • It is not necessary if all you need is the object py in reticulate, because you can create a global variable in the setup chunk, e.g.,

    ```{setup, include=FALSE}
    py <- reticulate::py
    ```
    

    If you don't do that, you'll have to library(reticulate) before you use py in subsequent R code chunks.