title: Jupyter notebook - rpy2 - Cannot find R libraries
body: <p>I am currently trying to use both R and Python in the same Jupyter Notebook. I successfully installed rpy2; if I try to write something in R (putting <code>%%R</code> at the beginning) everything works, but as soon as I try to use a library, the following error appears:</p>
  <blockquote>
  <p>R[write to console]: Error in library(name of the package) : there is no package
  called - name of the package -</p>
  </blockquote>
  <p>If I try to use the same library in R Studio (not in Jupyter) everything works.</p>
  <p>This is the code is giving me trouble:</p>
  <pre><code>import os
  os.environ['R_HOME'] = r'C:/PROGRA~1/R/R-40~1.0'
  os.environ['path'] += r';C:/PROGRA~1/R/R-40~1.0\bin;'
  %load_ext rpy2.ipython
  
  %%R
  library(readr)
  </code></pre>
  <p>After this last line the following error appears:</p>
  <blockquote>
  <p>R[write to console]: Error in library(readr) : there is no package called 'readr'</p>
  <p>Error in library(readr) : there is no package called 'readr'
  --------------------------------------------------------------------------- RRuntimeError                             Traceback (most recent call
  last)
  ~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\ipython\rmagic.py
  in eval(self, code)
  267                 # Need the newline in case the last line in code is a comment.
  --&gt; 268                 value, visible = ro.r(&quot;withVisible({%s\n})&quot; % code)
  269             except (ri.embedded.RRuntimeError, ValueError) as exception:</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\robjects_<em>init</em>_.py
  in <strong>call</strong>(self, string)
  415         p = rinterface.parse(string)
  --&gt; 416         res = self.eval(p)
  417         return conversion.rpy2py(res)</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\robjects\functions.py
  in <strong>call</strong>(self, *args, **kwargs)
  196                 kwargs[r_k] = v
  --&gt; 197         return (super(SignatureTranslatedFunction, self)
  198                 .<strong>call</strong>(*args, **kwargs))</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\robjects\functions.py
  in <strong>call</strong>(self, *args, **kwargs)
  124                 new_kwargs[k] = conversion.py2rpy(v)
  --&gt; 125         res = super(Function, self).<strong>call</strong>(*new_args, **new_kwargs)
  126         res = conversion.rpy2py(res)</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\rinterface_lib\conversion.py
  in _(*args, **kwargs)
  43     def _(*args, **kwargs):
  ---&gt; 44         cdata = function(*args, **kwargs)
  45         # TODO: test cdata is of the expected CType</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\rinterface.py in
  <strong>call</strong>(self, *args, **kwargs)
  623             if error_occured[0]:
  --&gt; 624                 raise embedded.RRuntimeError(_rinterface._geterrmessage())
  625         return res</p>
  <p>RRuntimeError: Error in library(readr) : there is no package called
  'readr'</p>
  <p>During handling of the above exception, another exception occurred:</p>
  <p>RInterpreterError                         Traceback (most recent call
  last)
  ~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\ipython\rmagic.py
  in R(self, line, cell, local_ns)
  762             else:
  --&gt; 763                 text_result, result, visible = self.eval(code)
  764                 text_output += text_result</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\ipython\rmagic.py
  in eval(self, code)
  271                 warning_or_other_msg = self.flush()
  --&gt; 272                 raise RInterpreterError(code, str(exception),
  273                                         warning_or_other_msg)</p>
  <p>RInterpreterError: Failed to parse and evaluate line
  'library(readr)\n'. R error message: &quot;Error in library(readr) : there
  is no package called 'readr'&quot;</p>
  <p>During handling of the above exception, another exception occurred:</p>
  <p>PermissionError                           Traceback (most recent call
  last)  in 
  ----&gt; 1 get_ipython().run_cell_magic('R', '', 'library(readr)\n')</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\site-packages\IPython\core\interactiveshell.py
  in run_cell_magic(self, magic_name, line, cell)    2379<br />
  with self.builtin_trap:    2380                 args = (magic_arg_s,
  cell)
  -&gt; 2381                 result = fn(*args, **kwargs)    2382             return result    2383</p>
  <p> in R(self, line, cell, local_ns)</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\site-packages\IPython\core\magic.py
  in (f, *a, **k)
  185     # but it's overkill for just that one bit of state.
  186     def magic_deco(arg):
  --&gt; 187         call = lambda f, *a, **k: f(*a, **k)
  188
  189         if callable(arg):</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\ipython\rmagic.py
  in R(self, line, cell, local_ns)
  782                 print(e.err)
  783             if tmpd:
  --&gt; 784                 rmtree(tmpd)
  785             return
  786         finally:</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\shutil.py in rmtree(path,
  ignore_errors, onerror)
  735             # can't continue even if onerror hook returns
  736             return
  --&gt; 737         return _rmtree_unsafe(path, onerror)
  738
  739 # Allow introspection of whether or not the hardening against symlink</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\shutil.py in _rmtree_unsafe(path,
  onerror)
  613                 os.unlink(fullname)
  614             except OSError:
  --&gt; 615                 onerror(os.unlink, fullname, sys.exc_info())
  616     try:
  617         os.rmdir(path)</p>
  <p>~\anaconda3\envs\Cattolica2020\lib\shutil.py in _rmtree_unsafe(path,
  onerror)
  611         else:
  612             try:
  --&gt; 613                 os.unlink(fullname)
  614             except OSError:
  615                 onerror(os.unlink, fullname, sys.exc_info())</p>
  <p>PermissionError: [WinError 32] Impossibile accedere al file. Il file è
  utilizzato da un altro processo:
  'C:\Users\User\AppData\Local\Temp\tmp82eo8sb4\Rplots001.png'</p>
  </blockquote>
  <p>I also tried to verify if the library directory is the same for Jupyter and R and I obtain the same two directories:</p>
  <blockquote>
  <p>[1] &quot;C:/Users/User/Documents/R/win-library/4.0&quot;
  [2] &quot;C:/Program Files/R/R-4.0.0/library</p>
  </blockquote>
  <p>I am currently using R 4.0.0 and Python 3.8.3</p>