简体   繁体   English

在 macOS 的 conda 环境下使用 R 包

[英]Working with an R package under a conda environment in macOS

I am trying to work with an R package that includes Rcpp + OpenMP in a conda environment in macOS.我正在尝试在 macOS 的 conda 环境中使用包含 Rcpp + OpenMP 的 R 包。 I read the conda environment documentation;我阅读了 conda 环境文档; however, I could not fix the problem I have in the following reproducible example.但是,我无法解决以下可重现示例中的问题。 Most of the documentation is based on addressing OpenMP issues (clang+llvm) on macOS.大多数文档都基于解决 macOS 上的 OpenMP 问题 (clang+llvm)。 I was wondering if there are any resources or documentation for the conda environment.我想知道 conda 环境是否有任何资源或文档。 These steps work on a Linux system (with conda) and macOS (without conda) without any problem.这些步骤适用于 Linux 系统(使用 conda)和 macOS(不使用 conda),没有任何问题。

Here is the reproducible example:这是可重现的示例:

In a macOS:在 macOS 中:

Step 1: Create a conda environment and install R:第 1 步:创建 conda 环境并安装 R:

conda create -n env r-essentials r-base

Step 2: activate the environment第二步:激活环境

conda activate env

Step 3: install rstudio第三步:安装rstudio

conda install -c r rstudio

Step 4: install some required packages第四步:安装一些需要的包

conda install -c r r-devtools
conda install -c r r-wcorr
conda install -c r r-ranger
conda install -c conda-forge r-rcpparmadillo
conda install -c r r-testthat
conda install -c conda-forge r-superlearner
conda install -c conda-forge r-polycore
conda install -c conda forge r-logger
conda install -c anaconda llvm
conda install -c conda-forge openmp

Step 5: Run rstudio第 5 步:运行 rstudio

Step 6: Inside rstudio第 6 步:在 rstudio 内部

library('devtools')
install_github('fasrc/CausalGPS')

I get the following error:我收到以下错误:

In file included from ColorSpace.cpp:1:
In file included from ./ColorSpace.h:4:
In file included from env/bin/../include/c++/v1/typeinfo:60:
In file included from env/bin/../include/c++/v1/exception:81:
In file included from env/bin/../include/c++/v1/cstdlib:85:
In file included from env/bin/../include/c++/v1/stdlib.h:100:
env/bin/../include/c++/v1/math.h:773:12: error: no member named 'labs' in the global namespace; did you mean 'abs'?
 return ::labs(__x);
     ~~^
~/env/bin/../include/c++/v1/math.h:772:39: note: 'abs' declared here
inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {
                   ^
~/env/bin/../include/c++/v1/math.h:777:12: error: no member named 'llabs' in the global namespace
 return ::llabs(__x);
     ~~^
~/env/bin/../include/c++/v1/math.h:785:12: error: no member named 'fabsf' in the global namespace
 return ::fabsf(__lcpp_x);
     ~~^
~/env/bin/../include/c++/v1/math.h:789:12: error: no member named 'fabs' in the global namespace; did you mean 'abs'?
 return ::fabs(__lcpp_x);
     ~~^
~/env/bin/../include/c++/v1/math.h:772:39: note: 'abs' declared here
inline _LIBCPP_INLINE_VISIBILITY long abs(long __x) _NOEXCEPT {
                   ^
~/env/bin/../include/c++/v1/math.h:794:12: error: no member named 'fabsl' in the global namespace
 return ::fabsl(__lcpp_x);
 ~~^

I think I need to set some environmental variables;我想我需要设置一些环境变量; however, I could not find out which variables I should export.但是,我无法找出应该导出哪些变量。 Do you have any idea?你有什么主意吗?

Works for me, with some adjustments that I regard as better practice:对我有用,经过一些我认为是更好的做法的调整:

  • don't use RStudio from Conda - it is an abandoned project;不要使用 Conda 的 RStudio - 这是一个废弃的项目; see alternatives查看替代方案
  • only use conda-forge channel - mixing channels can have dynamic library issues仅使用conda-forge通道 - 混合通道可能存在动态库问题
  • use a YAML for more reliable specification of requirements使用 YAML 以获得更可靠的需求规范
  • explicitly declare R version ( r-base )显式声明 R 版本( r-base
  • include everything in the declared Imports (except what is included as dependencies of other packages)包含声明的Imports所有内容(除了作为其他包的依赖项包含的内容)
  • conda-forge::r-cli>=3 builds are broken , so I pin that to newest working version conda-forge::r-cli>=3构建已损坏,因此我将其固定到最新的工作版本
  • use mamba because conda is slow使用mamba因为conda很慢

Here is a YAML for creating the environment:这是用于创建环境的 YAML:

causalgps-env.yaml causalgps-env.yaml

name: causalgps
channels:
  - conda-forge
dependencies:
  - r-base=4.1
  - r-tidyverse
  - r-devtools
  - r-xgboost
  - r-superlearner
  - r-earth
  - r-ranger
  - r-gam
  - r-kernsmooth
  - r-gnm
  - r-polycor
  - r-wcorr
  - r-rlang
  - r-glue
  - r-logger
  - r-cli>=2,<3

And the steps are:步骤是:

  1. Create env.创建环境。

     ## install Mamba if you don't have it ## conda install -n base conda-forge::mamba mamba env create -n causalgps -f causalgps-env.yaml
  2. Run R session in env.在 env 中运行 R 会话。

     conda activate causalgps R
  3. Install package.安装包。

     library(devtools) install_github('fasrc/CausalGPS')
  4. Test loading.测试加载。

     library(CausalGPS) ## works

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM