Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't import custom R package with importr #1057

Open
WilliWespe opened this issue Aug 18, 2023 · 3 comments
Open

Can't import custom R package with importr #1057

WilliWespe opened this issue Aug 18, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@WilliWespe
Copy link

I developed an R Package (lets call it 'myPackage') under Windows 10 / R 4.2.2
On my machine I could build a source package, install it, import it in Python and use it. Everything works well.
My colleague has to use it as well, but she uses a Mac and R 4.1.1. She could also build a source package on her machine and install it.
But when she tries to import it in Python an error gets thrown.

This is what we are trying:

from rpy2.robjects.packages import importr
from rpy2.robjects import r

# Set encoding to avoid errors with rpy2
r('Sys.setlocale("LC_ALL", "en_US.UTF-8")')

# import custom R package
importr('myPackage', lib_loc="/Library/Frameworks/R.framework/Versions/4.1/Resources/library")

We made sure, that the package actually was installed in the library location we specified. And as I said, when she ran library(myPackage) in RStudio, there was no error.

The error message we encountered is:

rpy2.rinterface_lib.embedded.RRuntimeError: Error in loadNamespace(name) : there is no package called 'myPackage'

Any idea whats the problem and how to solve it?

@WilliWespe WilliWespe added the bug Something isn't working label Aug 18, 2023
@lgautier
Copy link
Member

The error message we encountered is:

rpy2.rinterface_lib.embedded.RRuntimeError: Error in loadNamespace(name) : there is no package called 'myPackage'

That error comes from (the embedded) R. It genuinely does not know about your package.

Run the following in RStudio or wherever you can load that pacakge:

installed.packages()["myPackage", ]

It will tell you where it is installed. That path is not known to the embedded R in rpy2.

@WilliWespe
Copy link
Author

WilliWespe commented Aug 28, 2023

It will tell you where it is installed. That path is not known to the embedded R in rpy2.

Thank you very much, but how can I tell the embedded R in rpy2 where to look for it?
I thought I already did this with:
importr('myPackage', lib_loc="/Library/Frameworks/R.framework/Versions/4.1/Resources/library")

As I said we made sure, that the package actually was installed in the library location we specified

------EDIT-----

We ran installed.packages()["myPackage", ] and it told us the path we already knew: /Library/Frameworks/R.framework/Versions/4.1/Resources/library

Now my question: How to tell the embedded R in rpy2 this path?

@lgautier
Copy link
Member

lgautier commented Sep 1, 2023

There is always the possibility of rpy2's importr() not forwarding the argument about library location to the R function.

It does not seem to be the case though. At first sight R should get the information about the library location (although using a Python string as code to evaluate and trying to get the escaping of quotes is probable missing edge cases):
https://github.com/rpy2/rpy2/blob/master/rpy2/robjects/packages.py#L80

Can you try importr() with suppress_messages=False?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants