Jupyter笔记本rpy2找不到R库

2024-06-26 14:40:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正试图在同一个Jupyter笔记本中同时使用R和Python。我成功安装了rpy2;如果我尝试在R中写一些东西(将%%R放在开头),一切正常,但一旦我尝试使用库,就会出现以下错误:

R[write to console]: Error in library(name of the package) : there is no package called - name of the package -

如果我尝试在R Studio(而不是Jupyter)中使用相同的库,那么一切都会正常工作

这是给我带来麻烦的代码:

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)

在最后一行之后,出现以下错误:

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 2383

in 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'

我还尝试验证Jupyter和R的库目录是否相同,并获得了相同的两个目录:

[1] "C:/Users/User/Documents/R/win-library/4.0" [2] "C:/Program Files/R/R-4.0.0/library

我目前正在使用R4.0.0和Python3.8.3


Tags: inpyselflibpackageslibrarysiteargs
1条回答
网友
1楼 · 发布于 2024-06-26 14:40:19

异常RRuntimeError通常只是将R在执行期间自己生成的异常转发给Python

错误消息表示R找不到库。如果您确实确定RStudio和Jupyter都使用相同的R安装,那么两者之间的区别将来自RStudio被指示在比从Jupyter启动的R更多的目录中查找已安装的R包

在RStudio中运行以下命令以了解readr的加载位置:

library(dplyr)
as_data_frame(installed.packages()) %>%
  filter(Package == "readr") %>%
  select(Package, LibPath)

相关问题 更多 >