R, Python: Install Packages on Rpy2

install local package with rpy2 - invalid package

Unless you define it, Python will not know any NULL (hence the error). If you want to use R's NULL, you can find it as rpy2.robjects.NULL or rpy2.rinterface.NULL.

Otherwise, the documentation for the R function you want to use indicates that paths can also be specified with the prefix file://:

utils.install_packages('file://impactr_1.0.1.tar.gz', type = "source")

Using rpy2: cannot find R package that is installed

Okay, it was a pretty stupid thing:

R installs my packages at two locations on my computer: in an R folder in the Documents and in the actual program folder in Program Files. You can check your locations like this:

> .libPaths()
[1] "C:/Users/XYZ/Documents/R/win-library/3.6"
[2] "C:/Program Files/R/R-3.6.3/library"

rpy2 was only able to use the packages that were installed in location [2].

I simply moved everything from [1] to [2]. Now it works!

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

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'


Related Topics



Leave a reply



Submit