3

R built from source, installed locally. R is at ~/bin/R (which is in my PATH) and its libraries are in ~/lib64/R/. Installing rpy2 should be simple. It finds the correct R just fine (as it's in the path). Then it can't find the libraries.

$python setup.py build install
R version 3.2.1 (2015-06-18) -- "World-Famous Astronaut"

...

setup.py:211: UserWarning: No include specified
  warnings.warn('No include specified')
setup.py:222: UserWarning: No libraries as -l arguments to the compiler.
  warnings.warn('No libraries as -l arguments to the compiler.')

    Compilation parameters for rpy2's C components:
        include_dirs    = []
        library_dirs    = []
        libraries       = []
        extra_link_args = []

And then we get a million errors that it can't find functions that are in the R libraries.

Rpy2's documentation says there's a simple option for designating where R or its libraries are:

python setup.py build --r-home ~/lib64/R/lib install

But if you do this, then you get:

setup.py:222: UserWarning: No libraries as -l arguments to the compiler.
  warnings.warn('No libraries as -l arguments to the compiler.')

    Compilation parameters for rpy2's C components:
        include_dirs    = []
        library_dirs    = []
        libraries       = []
        extra_link_args = []

usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

error: option --r-home not recognized

It looks like the --r-home functionality has been removed. How does one point rpy2 to the correct libraries?


Edit:

Have now installed R with:

./configure --prefix=${HOME} --enable-R-shlib
make
make install

After that, I can install rpy2 with just pip install rpy2. But then, we're still having library problems:

import rpy2.robjects as robjects
ImportError: libRblas.so: cannot open shared object file: No such file or directory

So then I needed to add this to my path:

export LD_LIBRARY_PATH="~/lib64/R/lib:$LD_LIBRARY_PATH"

And then everything works!

jeffalstott
  • 2,643
  • 4
  • 28
  • 34
  • @Hack-R Why? I don't see how that relates. – jeffalstott Jun 18 '15 at 21:42
  • @Hack-R That is not what my question is about. I am asking about installing Rpy2, and how at installation it needs to know the location of R and its associated materials. At this point Rpy2 doesn't even install. – jeffalstott Jun 19 '15 at 14:27
  • Did you compile R as a shared library ? Also, any reason you are not doing `pip install rpy2` ? – lgautier Jun 20 '15 at 00:59
  • "Did you compile R as a shared library?" I do not know what you mean by that. I compiled it without root privileges, with everything in my ~/ directory. I'm not using `pip install rpy` because it produced the same error, and I thought that building from source would open up compilation options that I wouldn't otherwise have (like `--r-home`). – jeffalstott Jun 22 '15 at 18:17
  • During the configuration step, use `./configure --enable-R-shlib`. I also see that you have `libRblas.so`, `libRlapacks.so`, but no `libR.so`. May be sharing precisely how you compiled R would give helpful hints about what might be happening... – lgautier Jun 23 '15 at 02:07

3 Answers3

2

If R is in an unconventional location, the easiest might be help out a bit by setting environment variables (older versions of the doc is talking about --r-home but this was a less tested corner and it was removed).

Try:

export PATH=~/bin/R:${PATH}
export LD_LIBRARY_PATH=~/lib64/R/lib:${LD_LIBRARY_PATH}
export PKG_CONFIG_PATH=~/lib64/R/lib/pkgconfig/:${PKG_CONFIG_PATH}
lgautier
  • 11,363
  • 29
  • 42
  • Thanks! I just tried this, and it's the same behavior. The output still shows empty lists for the "Compilation parameters for rpy2's C components". Note: There is no `lib64/R/lib/pkgconfig` directory. All that's in `lib64/R/lib` is `libRblas.so` and `libRlapack.so` – jeffalstott Jun 19 '15 at 21:47
  • @lgautier's solution worked for me (without re-compilation of `R`). My specific problem was that I was using `Microsoft R open`. For my `MRO` version I changed `/etc/environment` to `LD_LIBRARY_PATH="your_old_stuff:/usr/lib64/MRO-3.2.3/R-3.2.3/lib/R/lib"`, `PATH="your_old_stuff:/usr/lib64/MRO-3.2.3/R-3.2.3/lib/R/bin"` and `PKG_CONFIG_PATH="your_old_stuff:/usr/lib64/MRO-3.2.3/R-3.2.3/lib/pkgconfig"`, with `your_old_stuff` denoting your old env variables. Might help someone with similar issues... Note that I was intentionally using `/etc/environment` as I have several users who call `Rpy2`. – cryo111 Jul 26 '16 at 13:03
  • @cryo111 : thanks for the note that rpy2 is also working with `Microsoft R Open` (I wanted to try out one day, and this since the Revolution Computing days, but never found the time to). – lgautier Jul 26 '16 at 22:18
1

I've had the best success building rpy2 with non-standard R locations using the following relative path during install:

export LDFLAGS="-Wl,-rpath,~/lib64/R/lib"

Note that you only need to export this in the terminal where you then run python setup.py install and not in your .bashrc or the like. It will store this library path in the rpy2 compiled parts.

This is imho a better option than LD_LIBRARY_PATH which some consider evil:

https://blogs.oracle.com/ali/entry/avoiding_ld_library_path_the

http://linuxmafia.com/faq/Admin/ld-lib-path.html

Midnighter
  • 3,771
  • 2
  • 29
  • 43
0

In addition to installing R for M1 Mac from https://cran.r-project.org/bin/macosx/

I also had to install wheel:

pip install wheel

(see Why is python setup.py saying invalid command 'bdist_wheel' on Travis CI?)

Muhteva
  • 2,729
  • 2
  • 9
  • 21