Reputation: 21
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 %%R
at the beginning) everything works, but as soon as I try to use a library, the following error appears:
R[write to console]: Error in library(name of the package) : there is no package called - name of the package -
If I try to use the same library in R Studio (not in Jupyter) everything works.
This is the code is giving me trouble:
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)
After this last line the following error appears:
R[write to console]: Error in library(readr) : there is no package called 'readr'
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. --> 268 value, visible = ro.r("withVisible({%s\n})" % code) 269 except (ri.embedded.RRuntimeError, ValueError) as exception:
~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\robjects_init_.py in call(self, string) 415 p = rinterface.parse(string) --> 416 res = self.eval(p) 417 return conversion.rpy2py(res)
~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\robjects\functions.py in call(self, *args, **kwargs) 196 kwargs[r_k] = v --> 197 return (super(SignatureTranslatedFunction, self) 198 .call(*args, **kwargs))
~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\robjects\functions.py in call(self, *args, **kwargs) 124 new_kwargs[k] = conversion.py2rpy(v) --> 125 res = super(Function, self).call(*new_args, **new_kwargs) 126 res = conversion.rpy2py(res)
~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\rinterface_lib\conversion.py in _(*args, **kwargs) 43 def _(*args, **kwargs): ---> 44 cdata = function(*args, **kwargs) 45 # TODO: test cdata is of the expected CType
~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\rinterface.py in call(self, *args, **kwargs) 623 if error_occured[0]: --> 624 raise embedded.RRuntimeError(_rinterface._geterrmessage()) 625 return res
RRuntimeError: Error in library(readr) : there is no package called 'readr'
During handling of the above exception, another exception occurred:
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: --> 763 text_result, result, visible = self.eval(code) 764 text_output += text_result
~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\ipython\rmagic.py in eval(self, code) 271 warning_or_other_msg = self.flush() --> 272 raise RInterpreterError(code, str(exception), 273 warning_or_other_msg)
RInterpreterError: Failed to parse and evaluate line 'library(readr)\n'. R error message: "Error in library(readr) : there is no package called 'readr'"
During handling of the above exception, another exception occurred:
PermissionError Traceback (most recent call last) in ----> 1 get_ipython().run_cell_magic('R', '', 'library(readr)\n')
~\anaconda3\envs\Cattolica2020\lib\site-packages\IPython\core\interactiveshell.py in run_cell_magic(self, magic_name, line, cell) 2379
with self.builtin_trap: 2380 args = (magic_arg_s, cell) -> 2381 result = fn(*args, **kwargs) 2382 return result 2383in R(self, line, cell, local_ns)
~\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): --> 187 call = lambda f, *a, **k: f(*a, **k) 188 189 if callable(arg):
~\anaconda3\envs\Cattolica2020\lib\site-packages\rpy2\ipython\rmagic.py in R(self, line, cell, local_ns) 782 print(e.err) 783 if tmpd: --> 784 rmtree(tmpd) 785 return 786 finally:
~\anaconda3\envs\Cattolica2020\lib\shutil.py in rmtree(path, ignore_errors, onerror) 735 # can't continue even if onerror hook returns 736 return --> 737 return _rmtree_unsafe(path, onerror) 738 739 # Allow introspection of whether or not the hardening against symlink
~\anaconda3\envs\Cattolica2020\lib\shutil.py in _rmtree_unsafe(path, onerror) 613 os.unlink(fullname) 614 except OSError: --> 615 onerror(os.unlink, fullname, sys.exc_info()) 616 try: 617 os.rmdir(path)
~\anaconda3\envs\Cattolica2020\lib\shutil.py in _rmtree_unsafe(path, onerror) 611 else: 612 try: --> 613 os.unlink(fullname) 614 except OSError: 615 onerror(os.unlink, fullname, sys.exc_info())
PermissionError: [WinError 32] Impossibile accedere al file. Il file è utilizzato da un altro processo: 'C:\Users\User\AppData\Local\Temp\tmp82eo8sb4\Rplots001.png'
I also tried to verify if the library directory is the same for Jupyter and R and I obtain the same two directories:
[1] "C:/Users/User/Documents/R/win-library/4.0" [2] "C:/Program Files/R/R-4.0.0/library
I am currently using R 4.0.0 and Python 3.8.3
Upvotes: 0
Views: 2220
Reputation: 11545
The exception RRuntimeError
is normally just forwarding to Python an exception that R generated itself during the execution.
The error message says that R does not find the library. If you are really sure that both RStudio and Jupyter use the very same R installed, the difference between the two will come from RStudio being instructed to look for installed R packages in more directories than the R started from Jupyter is.
Run the following in RStudio to know where readr
is loaded from:
library(dplyr)
as_data_frame(installed.packages()) %>%
filter(Package == "readr") %>%
select(Package, LibPath)
Upvotes: 0