I have used rpy2 to call R package successfully in python. However, I just install elasticnet today in R and I checked I can load the library in R without problem, also the package name folder elasticnet is in my directory.
I want to do the same call as other packages using importr, but it keeps giving me error message:
Error in loadNamepace(name): there is no package called 'elasticnet'.
But I have go to that address and actually see the package, also in R I can see that package is installed. Anyone know how to solve this problem?
This is how I called other packages in python, for example the pls package:
import rpy2
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
utils = importr("utils")
d = {'print.me': 'print_dot_me', 'print_me': 'print_uscore_me'}
pls = importr('pls', robject_translations = d, lib_loc = "/Users/Viii/Anaconda3/envs/rstudio/lib/R/library")
But when I replace that with elasticnet, it doesn't work.
And it gives me the error message saying
"RRuntimeWarning: 'package 'lars' was installed by an R version with
different internals; it needs to be reinstalled for use with this R
version'.
I also try to install glmnet instead, it also gives me the error message
en = importr('glmnet', robject_translations = d, lib_loc = "/Users/Viii/Anaconda3/envs/rstudio/lib/R/library")
Traceback (most recent call last):
File , line 1, in en =
importr('glmnet', robject_translations = d, lib_loc =
"/Users/Viii/Anaconda3/envs/rstudio/lib/R/library")
File
"C:\Users\Viii\Anaconda3\lib\site-packages\rpy2-2.9.4-py3.6-win-amd64.egg\rpy2\robjects\packages.py",
line 453, in importr env = _get_namespace(rname)
RRuntimeError: Error in loadNamespace(name) : there is no package
called 'glmnet'
The sessioninfo in R:
R version 3.5.0 (2018-04-23)
Platform: x86_64-conda_cos6-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS
Matrix products: default
BLAS/LAPACK: /home/viii/anaconda3/envs/rstudio/lib/R lib/libRblas.so
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods [7] base
other attached packages:
[1] elasticnet_1.1.1 lars_1.2
loaded via a namespace (and not attached):
[1] compiler_3.5.0 tools_3.5.0 yaml_2.1.18
Related
I need some help with using installed packages in PyWeka. I am able to install packages, but I am unable to use them or find where they are installed. When I try to find the full classname, i get an exception(which occurs when there is no matching module)
Example:
import weka.core.classes as core
core.complete_classname("J48")
Output
'weka.classifiers.trees.J48'
I am trying to install the DMNBtext package. Installation occurs but module cannot be found
import weka.core.classes as core
print(packages.is_installed("DMNBtext"))
core.complete_classname("DMNBtext")
Output
True
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-25-7ea05097d6f1> in <module>()
1 import weka.core.classes as core
2 print(packages.is_installed("DMNBtext"))
----> 3 core.complete_classname("DMNBtext")
/usr/local/lib/python3.6/dist-packages/weka/core/classes.py in complete_classname(classname)
1725 return str(result[0])
1726 elif len(result) == 0:
-> 1727 raise Exception("No classname matches found for: " + classname)
1728 else:
1729 matches = []
Exception: No classname matches found for: DMNBtext
Please note that is_installed gives True output, meaning the package is installed.
Any idea how I can resolve this ?
Also,my jvm was started with packages=True, so that should not be a problem.
Thanks in advance.
I just created a new virtual environment with python-weka-wrapper3:
virtualenv -p /usr/bin/python3.6 pww3
./pww3/bin/pip install numpy matplotlib pygraphviz javabridge python-weka-wrapper3
And then ran the following script successfully (needs to be run twice, if the DMNBtext package is not yet installed):
import sys
import weka.core.jvm as jvm
import weka.core.packages as packages
from weka.core.classes import complete_classname
jvm.start(packages=True)
pkg = "DMNBtext"
# install package if necessary
if not packages.is_installed(pkg):
print("Installing %s..." % pkg)
packages.install_package(pkg)
print("Installed %s, please re-run script!" % pkg)
jvm.stop()
sys.exit(0)
# testing classname completion
print(complete_classname(".J48"))
print(complete_classname(".DMNBtext"))
jvm.stop()
Once the DMNBtext package is installed, the script outputs this:
weka.classifiers.trees.J48
weka.classifiers.bayes.DMNBtext
I am going to assume you already have the pyWeka installed and running on Anaconda. The details of how to do this are here.
You should start activating the java virtual machine
import weka.core.jvm as jvm
import weka.core.classes as core
jvm.start(packages=True) # needed for package manipulation
from weka.classifiers import Classifier # you are going to classify something
import weka.core.packages as packages # so you can install packages
packages.install_package('DNMNBtext') # if it is not already installed.
# You can also install it on Weka
And now you can issue:
core.complete_classname("DMNBtext")
to find the name of the class
'weka.classifiers.bayes.DMNBtext'
Finally
dmnb = Classifier(classname="weka.classifiers.bayes.DMNBtext")
dmnb.options=[list of options]
Take care
I try to use an R package that I have installed using the R package 'packrat' that allow to create a virtual environment similar to virtuanlenv in python. But I do not succeed.
Within a console using R I can run successfully the following code:
cd /path/to/packrat/environment
R # this launch a R console in the packrat environment
library(mycustompackage)
result = mycustompackage::myfunc()
q()
I would like to do the same using rpy2, but I'm unable to activate the packrat environment. Here follow what I've tested unsuccessfully.
from rpy2.robjects import r
from rpy2.robjects.packages import importr
packrat_dir = r.setwd('/path/to/packrat/environment')
importr('mycustompackage')
result = r.mycustompackage.myfunc()
But it fails at 'importr' because it cannot find the package 'mycustompackage'. Either unsuccessfull :
importr('mycustompackage', lib_loc='/path/to/packrat/environment')
Neither:
os.environ['R_HOME'] = '/path/to/packrat/environment'
importr('mycustompackage', lib_loc ='/path/to/packrat/environment')
Any suggestion on how to use rpy2 with packrat environments?
I am not familiar with the R package packrat, but I am noticing that the bash + R and Python/rpy2 code have a subtle difference that might matter a lot: in the bash + R case, when R is starting it is already in your packrat project directory whereas in the Python / rpy2 case R is starting from a different directory and is moved to the packrat project directory using setwd().
I am reading that packrat is using a file .Rprofile (https://rstudio.github.io/packrat/limitations.html), evaluated by R at startup time if in the current directory. I suspect that the issue is down to how packrat is used rather than an issue with rpy2.
Very good remark (hidden file = forgotten file). I found out how to make it running:
from rpy2.robjects import r
from rpy2.robjects.packages import importr
# Init the packrat environment
r.setwd('/path/to/packrat/environment')
r.source('.Rprofile')
# use the packages it contains
importr('mycustompackage')
result = r.myfunc()
lgautier, you made my day, thanks a lot.
I'm trying to use the R (Version 3.4.3) package MICE in Python 3.6.5 with rpy2.
This is the code I use:
import os
os.environ['R_HOME'] = 'C:/Program Files/R/R-3.4.3'
os.environ['R_USER'] = 'C:/Users/username/Anaconda3/Lib/site-packages/rpy2'
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
mice = importr('mice', lib_loc ='C:\Program Files\R\R-3.4.3\library')
When I execute this, my Jupyter notebook dies. In the terminal I get the error message:
Error: package or namespace load failed for 'stats' in inDL(x, as.logical(local), as.logical(now), ...):
unable to load shared object 'C:/Program Files/R/R-3.4.3/library/stats/libs/x64/stats.dll':
LoadLibrary failure: The specified module could not be found.
During startup - Warning message:
package 'stats' in options("defaultPackages") was not found
Error: '\P' is an unrecognized escape in character string starting ""C:\P"
But I do have the package stats installed and the file 'C:/Program Files/R/R-3.4.3/library/stats/libs/x64/stats.dll' exists.
Does anybody know a solution for this problem?
I installed the package mice in RStudio. I made sure that all my installed R libraries are in the directory C:/Program Files/R/R-3.4.3/library so that dependencies of mice are found in the same folder. For this I just copied all the packages installed in another folder to that one (I hope that's not a problem?)
I installed umap-learn on mac OS and tried to use it in r markdown file using rPython the way it is explained here:
http://blog.schochastics.net/post/using-umap-in-r-with-rpython/#fn1
But when I run following code:
```{r}
umap <- function(x,n_neighbors=10,min_dist=0.1,metric="euclidean"){
x <- as.matrix(x)
colnames(x) <- NULL
rPython::python.exec( c( "def umap(data,n,mdist,metric):",
"\timport umap" ,
"\timport numpy",
"\tembedding = umap.UMAP(n_neighbors=n,min_dist=mdist,metric=metric).fit_transform(data)",
"\tres = embedding.tolist()",
"\treturn res"))
res <- rPython::python.call( "umap", x,n_neighbors,min_dist,metric)
do.call("rbind",res)
}
data(iris)
res <- umap(iris[,1:4])
```
I get the error:
Error in python.exec(python.command) : No module named umap
So, apparently Rstudio does not see the umap. I checked that the package is installed by conda list:
umap-learn 0.2.3 py36_0 conda-forge
How could I fix that?
Update
The version of python was wrong, so I added .Rprofile and made it point to the right version, however, the error persisted.
system("python --version")
Python 3.6.5 :: Anaconda, Inc.
Update
More detailed error (stack trace):
Error in python.exec(python.command) : No module named umap
4.stop(ret$error.desc)
3.python.exec(python.command)
2.rPython::python.call("umap", x, n_neighbors, min_dist, metric)
1.umap(iris[, 1:4])
You need to make sure that umap is available to the same Python used in R, which is not going to be your Anaconda installation by default.
You can check your Python with system("python --version") and if needed go on the cmd line and do pip install umap or pip3 install umap etc (to use the currently available Python; alternately you can switch the Python path to your Anaconda Python).
Hello i'm using the following code to install package through yum api , using pythongs cript i need to build some installation based on this code , now its installed but im getting some errors
#!/usr/bin/python
import sys
import platform
import urllib2, urllib
import re
import yum
package="ntp"
print ("Installing ntp")
print ("#################")
yb=yum.YumBase()
searchlist=['name']
arg=['ntp']
matches = yb.searchGenerator(searchlist,arg)
for (package, matched_value) in matches :
if package.name == 'ntp' : yb.install(package)
yb.buildTransaction()
yb.processTransaction()
errors i got after installation is done
Running rpm_check_debug
Traceback (most recent call last):
File "./test.py", line 29, in <module>
yb.processTransaction()
File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 4928, in processTransaction
self._doTestTransaction(callback,display=rpmTestDisplay)
File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 5027, in _doTestTransaction
raise Errors.YumTestTransactionError, errstring
yum.Errors.YumTestTransactionError: Test Transaction Errors: package ntp is already installed
even when i removed the ntp and run the script again its give me this error msg after finished installation
plus i want to adjust the installation process , to check if the package is already installed then print its already install and process to next step in the code , else process the installation steps ,
any tips also for for if condition in correct way using yum api
Hi the previous answer didn't work the correct one is the following:
import yum
yb=yum.YumBase()
inst = yb.rpmdb.returnPackages()
installed=[x.name for x in inst]
packages=['bla1', 'bla2', 'bla3']
for package in packages:
if package in installed:
print('{0} is already installed'.format(package))
else:
print('Installing {0}'.format(package))
kwarg = {
'name':package
}
yb.install(**kwarg)
yb.resolveDeps()
yb.buildTransaction()
yb.processTransaction()
You need to first check if the pacakge is installed, if so, then skip it:
yb.conf.cache = 1 # enable cache
installed = yb.rpmdb.returnPackages()
packages = ['a','b','c']
for package in packages:
if package in installed:
print('{0} is already installed, skipping...'.format(package))
else:
print('Installing {0}'.format(package))
yb.install(package)
yb.resolveDeps()
yb.buildTransaction()
yb.processTransaction()