Issue
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
R, Python: install packages on rpy2
from rpy2.robjects.packages import importr
utils = importr('utils')
utils.install_packages('DirichletReg')
but received the following RuntimeError
---------------------------------------------------------------------------
RRuntimeError Traceback (most recent call last)
<ipython-input-16-32acf37e1ef9> in <module>()
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?
Solution
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'
Answered By - Anonymous
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.