在conda上使用rpy2时,如何选择R安装



tl;dr我可以配置rpy2,使其加载conda安装而不是系统安装吗?

我的问题

当我运行rpy2时,它使用R的系统安装,但它从我的R的conda安装中内省并加载库。这有各种副作用,如分段故障等…

复制

我在Ubuntu 18服务器上安装了R,我的系统管理员已经安装了它。我想将conda与python和rpy2一起使用,以确保我可以在一定程度上自由地选择版本。

下面,您可以看到我在一个名为(rnvp)的conda环境中,rpy2标识R的4.0.5 conda安装,但rpy2.robjects库加载系统R安装,即4.0.3。

当运行situation脚本时,我得到以下

(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ python
Python 3.9.6 (default, Aug 18 2021, 19:38:01) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2.robjects as ro
During startup - Warning messages:
1: package ‘methods’ was built under R version 4.0.5 
2: package ‘datasets’ was built under R version 4.0.5 
3: package ‘utils’ was built under R version 4.0.5 
4: package ‘grDevices’ was built under R version 4.0.5 
5: package ‘graphics’ was built under R version 4.0.5 
6: package ‘stats’ was built under R version 4.0.5 
>>> print(ro.r)
<rpy2.robjects.R object at 0x7f8f16ece8b0>
platform: x86_64-pc-linux-gnu
arch: x86_64
os: linux-gnu
system: x86_64, linux-gnu
status: 
major: 4
minor: 0.3
year: 2020
month: 10
day: 10
svn rev: 79318
language: R
version.string: R version 4.0.3 (2020-10-10)
nickname: Bunny-Wunnies Freak Out
>>> import rpy2.situation
>>> for row in rpy2.situation.iter_info():
...     print(row)
... 
rpy2 version:
3.4.5
Python version:
3.9.6 (default, Aug 18 2021, 19:38:01) 
[GCC 7.5.0]
Looking for R's HOME:
Environment variable R_HOME: /home/ludhu126/miniconda3/envs/rnvp/lib/R
Calling `R RHOME`: /home/ludhu126/miniconda3/envs/rnvp/lib/R
Environment variable R_LIBS_USER: None
R's additions to LD_LIBRARY_PATH:
R version:
In the PATH: R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Loading R library from rpy2: OK
Additional directories to load R packages from:
None
C extension compilation:
include:
['/home/ludhu126/miniconda3/envs/rnvp/lib/R/include']
libraries:
['R', 'pcre2-8', 'lzma', 'bz2', 'z', 'rt', 'dl', 'm', 'iconv', 'icuuc', 'icui18n']
library_dirs:
['/home/ludhu126/miniconda3/envs/rnvp/lib', '/home/ludhu126/miniconda3/envs/rnvp/lib/R/lib', '/home/ludhu126/miniconda3/envs/rnvp/lib']
extra_compile_args:
[]
extra_link_args:
['-Wl,--export-dynamic', '-fopenmp', '-Wl,-O2', '-Wl,--sort-common', '-Wl,--as-needed', '-Wl,-z,relro', '-Wl,-z,now', '-Wl,--disable-new-dtags', '-Wl,--gc-sections', '-Wl,-rpath,/home/ludhu126/miniconda3/envs/rnvp/lib', '-Wl,-rpath-link,/home/ludhu126/miniconda3/envs/rnvp/lib']

终端指向R的conda安装,实际上是4.0.5

(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ which R
/home/ludhu126/miniconda3/envs/rnvp/bin/R
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ R --version
R version 4.0.5 (2021-03-31) -- "Shake and Throw"
Copyright (C) 2021 The R Foundation for Statistical Computing
Platform: x86_64-conda-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

在这种环境之外,我有来自/usr/lib/R的R 4.0.3

(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ conda activate base
(base) ludhu126@hyperion:~/real-nvp/real-nvp$ which R
/usr/bin/R
(base) ludhu126@hyperion:~/real-nvp/real-nvp$ R --version
R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under the terms of the
GNU General Public License versions 2 or 3.
For more information about these matters see
https://www.gnu.org/licenses/.

问题我可以配置rpy2以便它加载conda安装吗?

关闭重复

  • 如何使用默认R安装通过conda安装rpy2想要使用本地安装而不是conda安装。它也适用于python 2.7版本
  • 安装并使用RPy2(使用conda),使其使用/usr/lib/R中的默认R安装。R提到了其他问题,例如根本没有安装RPy2、绑定到lapack dll的问题或没有设置R_HOME变量。所有这些对我来说都没有问题
  • 如何将rpy2指向现有的R安装?表示PATH用于解决R安装。但我上面的CCD_ 7表明这不是问题
  • 强制conda环境中的rpy2在conda环境之外使用R是关于加载错误的R库。但这很好——它根据需要加载conda R安装库
  • 从rpy2导入robjects时,Getting Segmentation fault Core dumped error建议卸载基本R安装。我不是系统管理员,不能那样做

本地重新编译rpy2,使用链接器标志来说明要链接到哪个R库。

灵感来自博客文章https://thomas-cokelaer.info/blog/2012/01/installing-rpy2-with-different-r-version-already-installed/我决定从源代码重新编译rpy2,并将R路径作为环境变量提供给编译。

(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ export LDFLAGS="-Wl,-rpath,/home/ludhu126/miniconda3/envs/rnvp/lib/R/lib"
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ pip install rpy2 --force-reinstall --compile --no-binary rpy2
Collecting rpy2
Using cached rpy2-3.4.5.tar.gz (194 kB)
Collecting cffi>=1.10.0
Using cached cffi-1.14.6-cp39-cp39-manylinux1_x86_64.whl (405 kB)
Collecting jinja2
Using cached Jinja2-3.0.1-py3-none-any.whl (133 kB)
Collecting pytz
Using cached pytz-2021.1-py2.py3-none-any.whl (510 kB)
Collecting tzlocal
Using cached tzlocal-3.0-py3-none-any.whl (16 kB)
Collecting pycparser
Using cached pycparser-2.20-py2.py3-none-any.whl (112 kB)
Collecting MarkupSafe>=2.0
Using cached MarkupSafe-2.0.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
Skipping wheel build for rpy2, due to binaries being disabled for it.
Installing collected packages: pycparser, MarkupSafe, tzlocal, pytz, jinja2, cffi, rpy2
Attempting uninstall: pycparser
Found existing installation: pycparser 2.20
Uninstalling pycparser-2.20:
Successfully uninstalled pycparser-2.20
Attempting uninstall: MarkupSafe
Found existing installation: MarkupSafe 2.0.1
Uninstalling MarkupSafe-2.0.1:
Successfully uninstalled MarkupSafe-2.0.1
Attempting uninstall: tzlocal
Found existing installation: tzlocal 3.0
Uninstalling tzlocal-3.0:
Successfully uninstalled tzlocal-3.0
Attempting uninstall: pytz
Found existing installation: pytz 2021.1
Uninstalling pytz-2021.1:
Successfully uninstalled pytz-2021.1
Attempting uninstall: jinja2
Found existing installation: Jinja2 3.0.1
Uninstalling Jinja2-3.0.1:
Successfully uninstalled Jinja2-3.0.1
Attempting uninstall: cffi
Found existing installation: cffi 1.14.6
Uninstalling cffi-1.14.6:
Successfully uninstalled cffi-1.14.6
Attempting uninstall: rpy2
Found existing installation: rpy2 3.4.5
Uninstalling rpy2-3.4.5:
Successfully uninstalled rpy2-3.4.5
Running setup.py install for rpy2 ... done
Successfully installed MarkupSafe-2.0.1 cffi-1.14.6 jinja2-3.0.1 pycparser-2.20 pytz-2021.1 rpy2-3.4.5 tzlocal-3.0
(rnvp) ludhu126@hyperion:~/real-nvp/real-nvp$ python
Python 3.9.6 (default, Aug 18 2021, 19:38:01) 
[GCC 7.5.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rpy2.robjects as ro; print(ro.r('version'))
_                           
platform       x86_64-conda-linux-gnu      
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          0.5                         
year           2021                        
month          03                          
day            31                          
svn rev        80133                       
language       R                           
version.string R version 4.0.5 (2021-03-31)
nickname       Shake and Throw             
>>> 

运行

python -m rpy2.situation

可以告诉我们CCD_ 9是如何看待其环境的。除非指定了R_HOME,否则它将从PATH中的第一个R中选择它需要的内容。当以其他方式指定不匹配的LD_LIBRARY_PATH或预加载不匹配的共享R库时,可能会出现问题。

最新更新