Unable to install R package in python (Jupyter notebook)?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 12:59:48

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:

<environment: namespace:DirichletReg>

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