title: Reticulate - Running python chunks in Rmarkdown
body: <p>Maybe I'm missing something, but if the following code is the content of my Rmd file</p>
  
  <pre><code>```{r}
  library(reticulate)
  use_virtualenv("r-reticulate")
  py_available(TRUE)
  ```
  ```{python}
  a = 7
  print(a)
  ```
  ```{r}
  py$a
  ```
  </code></pre>
  
  <p>when I Knit the file, the output for the last chunk is 7 (as expected). On the other hand, clicking the run all button in Rstudio (or running chunks one by one), results on <code>NULL</code> for the last chunk. </p>
  
  <p>Comparing with the <a href="https://rstudio.github.io/reticulate/articles/rstudio_ide.html" rel="noreferrer">R notebook example</a> it seems like assigning something to <code>flights</code> in the python chunk should make <code>py$flights</code> available for R, but that doesn't seem the case.</p>
  
  <p>Questions: </p>
  
  <ol>
  <li>Is there a way to access from R a variable created in a Python chunk previously ran (not knit)? How to "export" to R a variable created within a python chunk? </li>
  <li>What is a good reference to understand what happens when I click the run button in a python chunk of a Rmarkdown file?</li>
  </ol>
  
  <p><strong>EDIT</strong>: Ok so after seeing the first answers here, I did update both knitr and rmarkdown to the latest version, but still had the same problem.
  I added <code>py_available(TRUE)</code> to my file to make sure it was initialized, still, last chunk results in <code>7</code> when knitted, but running chunks one-by-one results in </p>
  
  <pre><code>&gt; py$a
  Error in py_get_attr_impl(x, name, silent) : 
    AttributeError: 'module' object has no attribute 'a'
  </code></pre>
  
  <p>The problem is: Assigning a value to <code>a</code> in the python chunk isn't doing anything to <code>py$a</code> in the R environment. Maybe this "shared" environment between R and python isn't how the package is supposed to work? Also, for some extra information</p>
  
  <pre><code>&gt; py_config()
  python:         /usr/bin/python
  libpython:      /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
  pythonhome:     /usr:/usr
  version:        2.7.14 (default, Sep 23 2017, 22:06:14)  [GCC 7.2.0]
  numpy:          /usr/lib/python2.7/dist-packages/numpy
  numpy_version:  1.12.1
  
  python versions found: 
   /usr/bin/python
   /usr/bin/python3
  </code></pre>