Change R version when using rpy2 package in Jupyter notebook

痞子三分冷 提交于 2021-01-29 09:23:29

问题


I want to run a piece of R code with %%R in my python3-notebook. It works, but the problem is that the R version of the notebook is the 3.6.1, while the one in my RStudio is the 4.0.1. In fact when I run R.home() the paths are different. I tried to change the version using:

os.environ['R_HOME'] = '/Library/Frameworks/R.framework/Resources' 
%load_ext rpy2.ipython

but when I call it I have a lot of errors:

/Users/guglielmo/opt/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Error: package or namespace load failed for ‘methods’ in library.dynam(lib, package, package.lib):
 shared object ‘methods.dylib’ not found

  warnings.warn(x, RRuntimeWarning)
/Users/guglielmo/opt/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Error: package or namespace load failed for ‘utils’ in library.dynam(lib, package, package.lib):
 shared object ‘utils.dylib’ not found

  warnings.warn(x, RRuntimeWarning)
/Users/guglielmo/opt/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Error: package or namespace load failed for ‘grDevices’ in library.dynam(lib, package, package.lib):
 shared object ‘grDevices.dylib’ not found

  warnings.warn(x, RRuntimeWarning)
/Users/guglielmo/opt/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Error: package or namespace load failed for ‘graphics’ in library.dynam(lib, package, package.lib):
 shared object ‘grDevices.dylib’ not found

  warnings.warn(x, RRuntimeWarning)
/Users/guglielmo/opt/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Error: package or namespace load failed for ‘stats’ in library.dynam(lib, package, package.lib):
 shared object ‘grDevices.dylib’ not found

  warnings.warn(x, RRuntimeWarning)
/Users/guglielmo/opt/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: Durante l'avvio - 
  warnings.warn(x, RRuntimeWarning)
/Users/guglielmo/opt/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: There were 13 warnings (use warnings() to see them)
  warnings.warn(x, RRuntimeWarning)
/Users/guglielmo/opt/anaconda3/lib/python3.7/site-packages/rpy2/rinterface/__init__.py:146: RRuntimeWarning: 

  warnings.warn(x, RRuntimeWarning)
---------------------------------------------------------------------------
LookupError                               Traceback (most recent call last)
<ipython-input-4-fb23c6edefe4> in <module>
----> 1 get_ipython().run_line_magic('load_ext', 'rpy2.ipython')

~/opt/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2315                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2316             with self.builtin_trap:
-> 2317                 result = fn(*args, **kwargs)
   2318             return result
   2319 

<decorator-gen-64> in load_ext(self, module_str)

~/opt/anaconda3/lib/python3.7/site-packages/IPython/core/magic.py in <lambda>(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):

~/opt/anaconda3/lib/python3.7/site-packages/IPython/core/magics/extension.py in load_ext(self, module_str)
     31         if not module_str:
     32             raise UsageError('Missing module name.')
---> 33         res = self.shell.extension_manager.load_extension(module_str)
     34 
     35         if res == 'already loaded':

~/opt/anaconda3/lib/python3.7/site-packages/IPython/core/extensions.py in load_extension(self, module_str)
     78             if module_str not in sys.modules:
     79                 with prepended_to_syspath(self.ipython_extension_dir):
---> 80                     mod = import_module(module_str)
     81                     if mod.__file__.startswith(self.ipython_extension_dir):
     82                         print(("Loading extensions from {dir} is deprecated. "

~/opt/anaconda3/lib/python3.7/importlib/__init__.py in import_module(name, package)
    125                 break
    126             level += 1
--> 127     return _bootstrap._gcd_import(name[level:], package, level)
    128 
    129 

~/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py in _gcd_import(name, package, level)

~/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py in _find_and_load(name, import_)

~/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

~/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py in _load_unlocked(spec)

~/opt/anaconda3/lib/python3.7/importlib/_bootstrap_external.py in exec_module(self, module)

~/opt/anaconda3/lib/python3.7/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

~/opt/anaconda3/lib/python3.7/site-packages/rpy2/ipython/__init__.py in <module>
----> 1 from .rmagic import load_ipython_extension

~/opt/anaconda3/lib/python3.7/site-packages/rpy2/ipython/rmagic.py in <module>
     52 
     53 import rpy2.rinterface as ri
---> 54 import rpy2.robjects as ro
     55 import rpy2.robjects.packages as rpacks
     56 from rpy2.robjects.conversion import (Converter,

~/opt/anaconda3/lib/python3.7/site-packages/rpy2/robjects/__init__.py in <module>
     17 import rpy2.rlike.container as rlc
     18 
---> 19 from rpy2.robjects.robject import RObjectMixin, RObject
     20 from rpy2.robjects.vectors import (BoolVector,
     21                                    IntVector,

~/opt/anaconda3/lib/python3.7/site-packages/rpy2/robjects/robject.py in <module>
     50 
     51 
---> 52 class RObjectMixin(object):
     53     """ Class to provide methods common to all RObject instances. """
     54 

~/opt/anaconda3/lib/python3.7/site-packages/rpy2/robjects/robject.py in RObjectMixin()
     64     __rclass = rpy2.rinterface.baseenv.get("class")
     65     __rclass_set = rpy2.rinterface.baseenv.get("class<-")
---> 66     __show = rpy2.rinterface.baseenv.get("show")
     67 
     68     __slots = None

LookupError: 'show' not found

How can I solve the problem? I can compute a workaround installing the packages on R terminal, but also here I have different errors.

Thanks in advance

来源:https://stackoverflow.com/questions/65458168/change-r-version-when-using-rpy2-package-in-jupyter-notebook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!