Unable to load R packages

View from my favourite guest house in Kuchinoshima Island. A typical landscape of volcanic island.

R was installed using miniconda on my computer. Thus R packages are also installed via conda commands. Normally it works fine.

Sometimes, I generate new conda environments to keep analysis envirnoments for each research project. In such cases, there may be conflicts between R on the host and R on the conda environment.

Here are error messages I got:

library(annotatr)

ERROR: package or namespace load failed for ‘annotatr’ in library.dynam(lib, package, package.lib):

Shared object ‘Rcpp.dylib’ not found

library(dplyr)

ERROR: package or namespace load failed for ‘dplyr’ in library.dynam(lib, package, package.lib):

Shared object ‘rlang.dylib’ not found

These happened after activating a conda environment and installing R and R packages.

# conda create -n test -c conda-forge

# conda activate test

# conda config --env --add channels conda-forge

# conda install -c conda-forge r-base=3.6.1

# conda install -c r r-dplyr

# conda update r-dplyr

# ~/miniconda3/envs/cordblood/bin/R

Here I'm leaving a note with a possible solution since I cound not find a clear solution.

Because it is probably due to the conflict between host and the new environment, first check package-location(s). I found I have installed R from source that is conflicting conda-installed R.

.libPaths()

[1] "/Users/[my_name]/Library/R/3.6/library"

[2] "/Users/[my_name]/miniconda3/envs/test/lib/R/library"

If you have multiple locations for R packages like me, that is probably the cause of errors. You can avoid errors by specifying the location when you load packages..

library(dplyr,lib.loc="/Users/[my_name]/miniconda3/envs/test/lib/R/library")

Package loaded!