0

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   
Parfait
  • 104,375
  • 17
  • 94
  • 125
Vickyyy
  • 197
  • 1
  • 9
  • Did you specify a *lib_loc* in your attempted `importr("elasticnet")`? – Parfait Feb 14 '19 at 20:08
  • @Parfait yes I did exactly the same as I load PLS – Vickyyy Feb 14 '19 at 20:15
  • Are you running Python in a virtual environment that cannot see your local CPU environment: [Determine if Python is running inside virtualenv](https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv)? – Parfait Feb 14 '19 at 20:23
  • @Parfait Hi, I followed the post and it says I am outside virtualenv. – Vickyyy Feb 14 '19 at 21:44
  • Open R and just run `library(elasticnet, lib.loc="")`. Then print out `sessionInfo()`. Edit your above post with results. Let's see if `elasticnet` imports other packages it depends on. – Parfait Feb 14 '19 at 22:07
  • @Parfait And I got an 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'. But I just install this package today... – Vickyyy Feb 14 '19 at 22:08
  • @Parfait Because elasticnet depends on lars, so I also import lars = importr('lars', lib.loc=<>), and then it gives me this error message in the middle and also ended with Error in loadNamepace(name): there is no package called 'lars'. – Vickyyy Feb 14 '19 at 22:21
  • Is the `sessionInfo` run after `library(elasticnet, lib.loc=""`)? Please explicitly specify path. You may have multiple installations of *elasticnet* and *lars* across all `.libPaths()` but `rpy2` only sees one: https://stackoverflow.com/a/47003754/1422451. – Parfait Feb 15 '19 at 01:38
  • @Parfait Yes, and when I check .libPaths() there is only one path or my R. – Vickyyy Feb 15 '19 at 03:32
  • You might have two different R versions installed on machine. Compare home paths: 1) run this in R: `R.home()` and 2) run this in Python: `from rpy2.robjects.packages import importr; base = importr('base'); print(base.R_home())` (semicolons mean different lines). – Parfait Feb 15 '19 at 15:24
  • @Parfait Yes! In R it gives me "C:/Users/viii/ANACON~1/envs/rstudio/lib/R", but in python it gives "C:/Users/viii/Anaconda3/lib/R". I remember I installed spyder via Anaconda-Navigator and then I also install Rstudio here, but it says that it cannot be installed in the same environment and asked me to create a new env. Is this the problem? How should I solve it. – Vickyyy Feb 15 '19 at 18:15
  • I am not familiar with Anaconda-Navigator. My recommendation is to install everything (languages and IDEs) outside of package distributions. And do not tie languages with any IDEs which should be installed afterwards separately. Also, your file paths do not align as they reflect Windows but your `sessionInfo` reflects Linux Ubuntu! You definitely have mismatched envs going on. – Parfait Feb 15 '19 at 19:15
  • For a quick resolution, open the Rterm or R.exe at the rpy2 home path and run `install.packages("elasticnet")`. Like Python, R can run in terminal, not just RStudio. – Parfait Feb 17 '19 at 03:01
  • @Parfait, Thanks! Could you explain more about the quick solution (sorry I am pretty new to this)? What do you mean by "at the rpy2 home path"? – Vickyyy Feb 18 '19 at 19:35
  • In your terminal, start R at select location: `C:/Users/viii/Anaconda3/lib/R/R.exe` (rpy2 home). Then install packages(s). – Parfait Feb 18 '19 at 19:43

0 Answers0