10

I'm using Rpy2 on windows 7 64 and having trouble loading a package:

in R:

using(mi)

in python:

from rpy2.robjects.packages import importr
mi=importr('mi')

---------------------------------------------------------------------------
RRuntimeError                             Traceback (most recent call last)
<ipython-input-30-2d393a6df544> in <module>()
----> 1 mi=importr('mi')

C:\Anaconda\lib\site-packages\rpy2\robjects\packages.pyc in importr(name, lib_loc, robject_translations, signature_translation, suppress_messages, on_conflict, data)
    397     if _package_has_namespace(rname, 
    398                               _system_file(package = rname)):
--> 399         env = _get_namespace(rname)
    400         version = _get_namespace_version(rname)[0]
    401         exported_names = set(_get_namespace_exports(rname))

RRuntimeError: Error in loadNamespace(name) : there is no package called 'm

Any suggestions?

KGS
  • 277
  • 2
  • 4
  • 11
  • Are you sure to have rpy2 2.5.2 installed ? This could happen is ipython is fetching an older version of rpy2. – user 12321 Feb 06 '15 at 15:06
  • I installed using the 2.5.5 binary here: http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2 – KGS Feb 06 '15 at 16:04
  • didn't you get any error or warning for the installation? – user 12321 Feb 06 '15 at 16:08
  • I got errors initially, then I pip uninstall and re-installed with no errors. – KGS Feb 08 '15 at 16:41
  • You need install packages from Python. See these https://stackoverflow.com/questions/11561258/r-python-install-packages-on-rpy2 and https://stackoverflow.com/questions/32983365/rpy2-cannot-find-installed-external-r-packages/50442863#50442863 – Ivan Lipko May 21 '18 at 06:07

6 Answers6

10

I had a similar problem:

rpy2.rinterface.RRuntimeError: Error in loadNamespace(name) : there is no package called speedglm

I noticed that the issue is that rpy2 does not know the location of all R libraries. In my case, typing (in R)

.libPaths()

gave me

[1] "/home/nbarjest/R/x86_64-redhat-linux-gnu-library/3.4"
[2] "/usr/lib64/R/library"                                
[3] "/usr/share/R/library" 

While, typing (in Python 3)

import rpy2.rinterface
rpy2.rinterface.set_initoptions((b'rpy2', b'--no-save', b'--no-restore', b'--quiet'))
from rpy2.robjects.packages import importr
base = importr('base')
print(base._libPaths())

gave me only

[1] "/home/nbarjest/R/x86_64-redhat-linux-gnu-library/3.4"

I couldn't find a way to append the other two paths to base._libpath(). If you find a way to do it, please let me know. I used another workaround:

import rpy2
import rpy2.robjects as RObjects
from rpy2.robjects.packages import importr
utils = importr("utils")
d = {'print.me': 'print_dot_me', 'print_me': 'print_uscore_me'}
try:
    thatpackage = importr('speedglm', robject_translations = d, lib_loc = "/home/nbarjest/R/x86_64-redhat-linux-gnu-library/3.4")
except:
    try:
        thatpackage = importr('speedglm', robject_translations = d, lib_loc = "/usr/lib64/R/library")
    except:
        thatpackage = importr('speedglm', robject_translations = d, lib_loc = "/usr/share/R/library")

This works. I hope other people who have the same problem find this useful.

Nbarjest
  • 691
  • 6
  • 9
  • To get the library path in Python3 I got an error using the above code snippet. To fix that, I just had to remove the second line: `# rpy2.rinterface.set_initoptions((b'rpy2', b'--no-save', b'--no-restore', b'--quiet'))` – Johann Oct 25 '19 at 12:31
  • @Nbarjest, since then, have you found another way to edit `base._libpath()`? Have you noticed `base._expand_R_libs_env_var()`? – Johan May 08 '23 at 09:15
3

For me, in importr, the argument lib_loc inside it worked, putting the first path that appears in the output of .libPaths() in R, like:

importr('name package', lib_loc="/home/nbarjest/R/x86_64-redhat-linux-gnu-library/3.4"),

where the path is the path in the output example of the @Nbarjest answer.

3

In python: Check the version of R being used by rpy2

import rpy2.robjects as robjects
robjects.r['version']

Check your rpy2 library location

base = importr('base')
print(base._libPaths())

In R: Check your R library location for this version of r

.libPaths()

copy the library installed in your version of r to the folder used by rpy2.

ArcticCzar
  • 41
  • 2
0

I also have this problem,and i copy the package i need to base._libPaths() ,here , and it works.

import rpy2.robjects as objects
from rpy2.robjects.packages import importer
base = importr('base')
base._libPaths()[0]
0

I had a similar problem. I had to uninstall R and reinstall it with admin rights, then reinstall the R package while running R with admin rights, so it would install to the standard library location (not a personal library). Then add R to the PATH variable, and reinstall rpy2.

Alex
  • 55
  • 6
-3

This is was cross-posted, and answered, on the issue tracker for rpy2: https://bitbucket.org/rpy2/rpy2/issue/265/windows-error-in-loadnamespace

lgautier
  • 11,363
  • 29
  • 42