Working with an R package under a conda environment in macOS - r

I am trying to work with an R package that includes Rcpp + OpenMP in a conda environment in macOS. I read the conda environment documentation; 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. I was wondering if there are any resources or documentation for the conda environment. These steps work on a Linux system (with conda) and macOS (without conda) without any problem.
Here is the reproducible example:
In a macOS:
Step 1: Create a conda environment and install R:
conda create -n env r-essentials r-base
Step 2: activate the environment
conda activate env
Step 3: install 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
Step 6: Inside 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; see alternatives
only use conda-forge channel - mixing channels can have dynamic library issues
use a YAML for more reliable specification of requirements
explicitly declare R version (r-base)
include everything in the declared Imports (except what is included as dependencies of other packages)
conda-forge::r-cli>=3 builds are broken, so I pin that to newest working version
use mamba because conda is slow
Here is a YAML for creating the environment:
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:
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
Run R session in env.
conda activate causalgps
R
Install package.
library(devtools)
install_github('fasrc/CausalGPS')
Test loading.
library(CausalGPS) ## works

Related

R fails to load igraph in conda environment

I am trying to run an RShiny app in a WSL2 installation of Ubuntu on Windows. I am no expert in R, but I feel this is a problem due to conda interaction with R. I run the following commands:
conda create -n r_env r-essentials r-base
conda activate r_env
conda install -c conda-forge r-shiny
conda install -c r r-visnetwork
conda install -c conda-forge r-dplyr
conda install -c r r-dt
conda install -c conda-forge r-igraph
conda install -c r r-leaflet
conda install -c conda-forge r-rgdal
conda install -c r r-shinydashboard
conda install -c conda-forge r-shinywidgets
conda install -c conda-forge r-shinycssloaders
conda install -c conda-forge r-igraph
When I run R and type in : library(igraph) i get:
->Error: package or namespace load failed for ‘igraph’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/home/carlo/anaconda3/envs/r_env/lib/R/library/igraph/libs/igraph.so':
but I can list it, it's there:
ll /home/carlo/anaconda3/envs/r_env/lib/R/library/igraph/libs/igraph.so
-> -rwxrwxr-x 1 carlo carlo 3816608 Mar 31 15:38 /home/carlo/anaconda3/envs/r_env/lib/R/library/igraph/libs/igraph.so
Did anybody encounter a similar problem?
The igraph library was meant to load correctly
(My Opinion) I would caution against the use of the r channel and the r-essentials package. The Continuum/Anaconda support for R was a good college-try, but is since outmoded and superseded by the broader CRAN support that Conda Forge provides. Users managing R environments will find a better experience ignoring any Continuum/Anaconda documentation and exclusively using Conda Forge for their R environments. (End Opinion)
Mixing channels can lead to symbol reference errors. Furthermore, sequences of ad hoc installations are subpar - instead specify through a YAML.
The following YAML file works just fine on linux-64, osx-64, and win-64 platforms:
so-igraph.yaml
name: so-igraph
channels:
- conda-forge
dependencies:
- r-base=4.1 # adjust to desired version
- r-shiny
- r-visnetwork
- r-dplyr
- r-dt
- r-igraph
- r-leaflet
- r-rgdal
- r-shinydashboard
- r-shinywidgets
- r-shinycssloaders
- r-igraph
Which can be used with
conda env create -n so-igraph -f so-igraph.yaml
conda activate so-igraph
I was having a similar problem with R on AlmaLinux, and it turned out I was missing some libraries on the OS itself, which I thought I had and that were necessary for some R packages. I think they were these, which makes sense, given that we're talking about igraph, a graphing package:
gsl-devel-2.5-1.el8.x86_64
gsl-2.5-1.el8.x86_64
openssl-1.1.1k-6.el8_5.x86_64
geos-devel-3.7.2-1.el8.x86_64
geos-3.7.2-1.el8.x86_64
proj-datumgrid-1.8-6.3.2.4.el8.noarch
proj-6.3.2-4.el8.x86_64
libtiff-devel-4.0.9-21.el8.x86_64
libgeotiff-devel-1.5.1-1.el8.x86_64
libgeotiff-1.5.1-1.el8.x86_64
Installing openblas may work, see https://github.com/conda-forge/r-igraph-feedstock/issues/19

Issue installing R package 'devtools' in Docker - getting error installing git2r

I am using a continuumio/miniconda3:latest base container and activating a Conda environment.
This all works fine.
When I run:
RUN R -e "install.packages('devtools',repos = 'http://cran.us.r-project.org')"
I get an error:
configure: Package dependency requirement 'libgit2 >= 0.26.0' could not be satisfied.
-----------------------------------------------------------------------
Unable to find the libgit2 library on this system. Building 'git2r'
using the bundled source of the libgit2 library.
To build git2r with a system installation of libgit2, please install:
libgit2-dev (package on e.g. Debian and Ubuntu)
libgit2-devel (package on e.g. Fedora, CentOS and RHEL)
libgit2 (Homebrew package on OS X)
and try again.
If the libgit2 library is installed on your system but the git2r
configuration is unable to find it, you can specify the include and
lib path to libgit2 with:
given you downloaded a tar-gz archive:
R CMD INSTALL git2r-.tar.gz --configure-vars='INCLUDE_DIR=/path/to/include LIB_DIR=/path/to/lib'
or cloned the GitHub git2r repository into a directory:
R CMD INSTALL git2r/ --configure-vars='INCLUDE_DIR=/path/to/include LIB_DIR=/path/to/lib'
or download and install git2r in R using
install.packages('git2r', type='source', configure.vars='LIB_DIR=-L/path/to/libs INCLUDE_DIR=-I/path/to/headers')
On macOS, another possibility is to let the configuration
automatically download the libgit2 library from the Homebrew
package manager with:
R CMD INSTALL git2r-.tar.gz --configure-vars='autobrew=yes'
or
R CMD INSTALL git2r/ --configure-vars='autobrew=yes'
or
install.packages('git2r', type='source', configure.vars='autobrew=yes')
-----------------------------------------------------------------------
configure: Attempting configuration of bundled libgit2
checking size of void*... 8
checking for library containing inflate... no
configure: error: in `/tmp/Rtmpbkmfhr/R.INSTALL8c407eb001/git2r':
configure: error:
---------------------------------------------
The zlib library that is required to build
git2r was not found.
Please install:
zlib1g-dev (package on e.g. Debian and Ubuntu)
zlib-devel (package on e.g. Fedora, CentOS and RHEL)
and try again.
If the zlib library is installed on your
system but the git2r configuration is
unable to find it, you can specify the
include and lib path to zlib with:
R CMD INSTALL git2r --configure-vars='LIBS=-L/path/to/libs CPPFLAGS=-I/path/to/headers'
---------------------------------------------
See `config.log' for more details
ERROR: configuration failed for package ‘git2r’
* removing ‘/opt/conda/envs/r-app/lib/R/library/git2r’
I have libgit2-dev installed at the start of my Dockerfile using apt-get install libgit2-dev and it installs libgit2 version 0.27.7.
Now the odd part about this is if I start the container and access it, I can run the "install.packages('devtools',repos = 'http://cran.us.r-project.org')" just fine and it works.
Dockerfile:
FROM continuumio/miniconda3:latest
RUN apt-get update -y; apt-get upgrade -y; \
apt-get install -y vim ssh libgit2-dev zlib1g-dev \
build-essential gcc gfortran g++
RUN conda update -n base -c defaults conda
COPY environment.yml environment.yml
RUN conda env create -f environment.yml
RUN echo "source activate r-env" >> ~/.bashrc
ENV PATH /opt/conda/envs/r-env/bin:/opt/conda/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
RUN R -e "install.packages('devtools',repos = 'http://cran.us.r-project.org')"
environment.yml:
name: r-env
channels:
- conda-forge
- defaults
dependencies:
- python=3.7
- r-essentials
- r-base
Replace the last line of your Dockerfile with:
RUN Rscript -e "install.packages('devtools',repos = 'http://cran.us.r-project.org')"

Failing to conda build R package

I'm trying to use conda and Jupypter to write some R code. I found a useful package called 'treatSens' and tried to install via conda following the instructions here.
conda install conda-build
conda skeleton cran treatSens
conda build r-treatsens
conda install -c local r-treatsens
Then I got this error conda_build.exceptions.DependencyNeedsBuildingError: Unsatisfiable dependencies for platform osx-64: {"r-dbarts[version='>=0.9_1']"}. How do I satisfy this dependency? Any suggestion is appreciated.
===UPDATE====
Added --recursive then I got a new error, posted a new question
here.
Try the following:
conda install conda-build
conda skeleton cran --recursive treatSens
conda build r-treatsens
conda install -c local r-treatsens
The recursive option should account for the dependencies you need.

How to install Hotelling (R package) with conda?

I am trying to install this package to use with R in conda:
Hotelling
However, I get this error message with the conda install:
$ conda install Hotelling
Fetching package metadata .................
PackageNotFoundError: Packages missing in current channels:
hotelling
We have searched for the packages in the following channels:
https://conda.anaconda.org/bioconda/osx-64
https://conda.anaconda.org/bioconda/noarch
https://conda.anaconda.org/conda-forge/osx-64
https://conda.anaconda.org/conda-forge/noarch
https://repo.continuum.io/pkgs/main/osx-64
https://repo.continuum.io/pkgs/main/noarch
https://repo.continuum.io/pkgs/free/osx-64
https://repo.continuum.io/pkgs/free/noarch
https://repo.continuum.io/pkgs/r/osx-64
https://repo.continuum.io/pkgs/r/noarch
https://repo.continuum.io/pkgs/pro/osx-64
https://repo.continuum.io/pkgs/pro/noarch
https://conda.anaconda.org/r/osx-64
https://conda.anaconda.org/r/noarch
It isn't available on any anaconda repos. You can add it to a private repo and then install it using conda. Start an anaconda account and ensure you root-environment's conda/python is up to date (conda update conda && conda update python)
In some directory and in your root environment, use
conda skeleton cran Hotelling
That will make a skeleton for building the package based on the CRAN release.
Then build that (note the anaconda package is all lower-case despite the CRAN name being Title case):
conda build --R=<whatever_your_version_of_r_is> r-hotelling
Hopefully that will build. If it's succesful it should give you a message that tells you how to upload to your account on anaconda-cloud
anaconda upload <path_to_your_built_r-hotelling> -u <your_account_name>
Then install it:
conda install -c <your_account_name> r-hotelling

How to install Julia in an anaconda environment?

One of the main features of Anaconda is that it is language agnostic as stated in their blog:
You can create environments of any binary dependency tree (different
versions of Python, R, Julia, etc.).
Recently I switched from using virtualenv to Anaconda in Python, so I was curious to try Julia in an Anaconda environment. However, I couldn't find instructions explicit enough to install Julia successfully. First, I tried naively conda create -n julia-test julia. Obviously, it didn't work. Then I found at binstar.org a Julia package (version 0.3) with the code
conda install -c https://conda.binstar.org/wakari1 julia
However, I don't want to install Julia outside of a specific virtual environment, so I changed it to:
conda create -n julia-test -c https://conda.binstar.org/wakari1 julia
It didn't throw errors but ultimately failed to start the Julia interpreter.
So, what is the correct way of installing Julia (0.2, preferably) in an anaconda environment?
UPDATE
As of March 2018, Julia v0.6.1 is available for linux-64 on the conda-forge channel:
https://anaconda.org/conda-forge/julia
It has been set up to install packages inside <env_prefix>/share/julia/site, to maintain isolation from the user's ~/.julia user's home directory.
conda create -n julia -c conda-forge julia
As of August 2017, Julia v0.5.2 is available on the conda-forge channel:
https://anaconda.org/conda-forge/julia
It has been set up to install packages inside <env_prefix>/share/julia/site, to maintain isolation from the user's ~/.julia user's home directory.
conda create -n julia -c conda-forge julia
The blog post was indicating that conda is general enough to allow packages of any type. There are no packages for Julia yet (except for the one you found in the Wakari channel, which is specific to Wakari).
Building a conda package for Julia and probably isn't difficult. Building a streamlined way to convert Julia packages into conda packages is a bit more work.
Julia 0.4.5 (not the current latest 0.5.0) is now available from the bioconda channel.
Using anaconda (python 3.6 version) and following instructions in bioconda :
# In this order
conda config --add channels conda-forge
conda config --add channels defaults
conda config --add channels r
conda config --add channels bioconda
conda install julia
So to create the corresponding virtual environment:
conda create -n julia-env julia
Nonetheless, I did not see any additional julia libraries available yet.
As of Jan 2022, Anaconda suggests using;
conda install -c conda-forge julia
See: https://anaconda.org/conda-forge/julia

Resources