jupyter安装python包,无法在python(Jupyter笔记本)中安装R包?

I am trying to install R package on python 3x in the jupyter notebook.

I understand that I have to pip install rpy2 and it has been successful

This works fine when I call a built-in function in R such as ccf or other easy issues.

# Call function from R

import os

os.environ['R_USER'] = 'D:\Anaconda3\Lib\site-packages\rpy2'

import rpy2.robjects as robjects

from rpy2.robjects import pandas2ri

pandas2ri.activate()

However, if I want to install a package such as DirichletReg or vars, it is not that easy especially that there might be more packages that are required to be downloaded.

I indeed followed the link as described in

from rpy2.robjects.packages import importr

utils = importr('utils')

utils.install_packages('DirichletReg')

but received the following RuntimeError

---------------------------------------------------------------------------

RRuntimeError Traceback (most recent call last)

in ()

1 from rpy2.robjects.packages import importr

2 utils = importr('utils')

----> 3 utils.install_packages('DirichletReg')

D:\Anaconda3\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)

176 v = kwargs.pop(k)

177 kwargs[r_k] = v

--> 178 return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)

179

180 pattern_link = re.compile(r'\\link\{(.+?)\}')

D:\Anaconda3\lib\site-packages\rpy2\robjects\functions.py in __call__(self, *args, **kwargs)

104 for k, v in kwargs.items():

105 new_kwargs[k] = conversion.py2ri(v)

--> 106 res = super(Function, self).__call__(*new_args, **new_kwargs)

107 res = conversion.ri2ro(res)

108 return res

RRuntimeError: Error in (function (pkgs, lib, repos = getOption("repos"), contriburl = contrib.url(repos, :

py2/R/win-library/3.3'\Anaconda3\Lib\site-packages

did anyone found this difficulty earlier?

解决方案

Jupyter Notebook Users (Windows)

1) It seems that what I have been gone through was that the R library was not in the same directory as in the python library

2) It seems that some packages need to be installed in R first

To solve this requires 2 Major steps one in R and the other in Python Jupyter notebook

Step1: Go to R (Rstudio)

Code:

install.packages('DirichletReg', dep = TRUE)

This will show you that

package ‘httpuv’ successfully unpacked and MD5 sums checked

package ‘xtable’ successfully unpacked and MD5 sums checked

package ‘sourcetools’ successfully unpacked and MD5 sums checked

package ‘htmlwidgets’ successfully unpacked and MD5 sums checked

package ‘shiny’ successfully unpacked and MD5 sums checked

package ‘miscTools’ successfully unpacked and MD5 sums checked

package ‘rgl’ successfully unpacked and MD5 sums checked

package ‘maxLik’ successfully unpacked and MD5 sums checked

package ‘DirichletReg’ successfully unpacked and MD5 sums checked

Then load the package in R as

> loadNamespace('DirichletReg')

It will give an output as:

double check the directory by coding in R:

R.home()

Check the output as

"C:/PROGRA~1/R/R-33~1.3"

Trick!!!

This is not the place where R is downloading the packages to. You can see where it is downloading to by coding in R:

.libPaths()

Say the outcome is XYZ (copy this)

Step 2: Go to Jupyter notebook

Check the current R directory (I assume you have rpy2 already installed)

import rpy2

import os

os.environ['R_USER'] = 'D:\Anaconda3\Lib\site-packages\rpy2'

from rpy2.robjects.packages import importr

base = importr('base')

print(base.R_home())

the output will be

"C:/Program Files/R/R-3.3.3"

Hence does not match with the R library directory where packages are in XYZ

hence to import or install the new package all required is to

DirichletReg = importr("DirichletReg", lib_loc = "XYZ")

This will usaually work like it work with me for all others

mi = importr("mi", lib_loc = "XYZ")

ggplot2 = importr("ggplot2", lib_loc = "XYZ")

But it did not work for DirichletReg it gave me the error

RRuntimeError: Error in loadNamespace(name) : there is no package called 'ggplot2'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值