将R软件包导入Sypder,rpy2模块,OS X

时间:2017-11-04 12:39:35

标签: python r macos anaconda rpy2

我已经安装了rpy2但是我无法导入除了内置R包之外的任何R包。这里有更多细节。我的操作系统是OS X El Capitan。我使用anaconda和Spyder 3.2.4,R版本3.4.2。我想问题是Spyder引用库的路径与安装R库的路径不同。这是我从R(RStudio)

获得的路径信息
.libPaths()
"/Library/Frameworks/R.framework/Versions/3.2/Resources/library"
 R.home()
"/Library/Frameworks/R.framework/Resources"

以及我从Spyder获得的路径信息

base= rpackages.importr('base')
print(base.R_home())
[1] "/Users/Me/anaconda3/lib/R"
print(base._libPaths())
[1] "/Users/Me/anaconda3/lib/R/library"

以下是安装软件包的代码

import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
utils = rpackages.importr('utils')

utils.chooseCRANmirror(ind=1) # select the first mirror in the list
utils.chooseBioCmirror(ind=1) # select the first mirror in the list
packnames = ('e1071')
from rpy2.robjects.vectors import StrVector
utils.install_packages(StrVector(packnames))

输出

warnings.warn(x, RRuntimeWarning)
/Users/Me/anaconda3/lib/python3.6/site- 
packages/rpy2/rinterface/__init__.py:145: RRuntimeWarning: Making       
'packages.html' ...
 warnings.warn(x, RRuntimeWarning)
 /Users/Me/anaconda3/lib/python3.6/site-  
 packages/rpy2/rinterface/__init__.py:145: RRuntimeWarning:  done

 warnings.warn(x, RRuntimeWarning)
Out[1]: rpy2.rinterface.NULL

导入包时我得到了

RRuntimeError: Error in loadNamespace(name) : there is no package called ‘e1071’

我还尝试手动将软件包从R库复制并粘贴到“/ Users / Me / anaconda3 / lib / R”,但这会导致内核崩溃。我还尝试通过

将rpy2指向R库
Forcast=   
rpackages.importr('e1071',lib_loc="/Library/Frameworks/R.framework/Versions/3.2/Resources/library")

内核崩溃了。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的错误,尝试了同样的事情,并且遇到了相同的内核崩溃问题。在我的情况下,我试图导入"预测"包。然后我打开了Anaconda-Navigator,从那里安装了RStudio,并安装了"预测"在RStudio包。现在我可以用Python导入包了。