创建环境

conda env list #查看已有环境

conda create -n R3.6

conda activate R3.6

# 如果激活环境失败,重新进入虚拟环境
source activate
# 退出虚拟环境
conda deactivate

添加镜像

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
conda config --set show_channel_urls yes
conda config --show
conda search r-base

安装R和R包

conda install r-base=3.6.1
which -a R

设置镜像

进入R

file.edit(file.path("~",".Rprofile"))  #打开一个文件

options("repos"=c(CRAN="https://mirrors.pku.edu.cn/CRAN/","The Comprehensive R Archive Network","http://mirrors.aliyun.com/CRAN"))

写完后,退出,重新进入R

options()$repos      #查看现有镜像

安装BiocManager

if (!require("BiocManager",quietly = TRUE))
install.packages("BiocManager")
BiocManager::install(xcms)

MASS等无法通过R直接安装的可以用conda安装

conda install -c conda-forge r-MASS
conda install -c conda-forge r-lattice
conda install -c conda-forge r-XML
conda install -c conda-forge r-Matrix
conda install -c conda-forge r-MALDIquant

安装R包netcdf4错误

Ubuntu Error: package or namespace load failed for ‘ncdf4’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/home/miniconda3/envs/R3.6/lib/R/library/00LOCK-ncdf4/00new/ncdf4/libs/ncdf4.so':
  /usr/lib/x86_64-linux-gnu/libp11-kit.so.0: undefined symbol: ffi_type_pointer, version LIBFFI_BASE_7.0
Error: loading failed
Execution halted
ERROR: loading failed

新建的conda环境以R3.6.3为基础,重新安装xcms包,遇到各种问题,查询Google未果,最终还是ChatGPT解决了,记录一下吧

The error message indicates that there is a problem with loading the ncdf4.so shared object file due to an issue with a specific library (libp11-kit.so.0). The root of the issue is related to an undefined symbol (ffi_type_pointer) in the libp11-kit.so.0 library, which suggests a conflict or mismatch between the versions of libraries used in your environment.

This could be happening because of conflicting versions of libraries managed by conda and the system libraries (libffi and libp11-kit), or possibly because of issues with your R environment setup.

Here are steps to help resolve this issue:

1. Ensure libffi is Installed and Correct Version is Present

The symbol ffi_type_pointer is part of libffi, so first, make sure that libffi is installed and correctly linked:

sudo apt update
sudo apt install libffi-dev

2. Rebuild the NetCDF Library and Ensure Compatibility

In conda environments, library versions may sometimes conflict with system libraries. Rebuild the environment’s libraries to ensure consistency:

conda update --all

Additionally, ensure that libnetcdf is installed in the conda environment:

conda install -c conda-forge libnetcdf

3. Verify the Correct libp11-kit is Installed

The error mentions libp11-kit.so.0, so ensure that this library is properly installed:

sudo apt install libp11-kit0

You can also try reinstalling it to ensure that the correct version is present:

sudo apt reinstall libp11-kit0

4. Set the LD_LIBRARY_PATH to Avoid Conflicts

You might be encountering conflicts between the system’s libraries and those installed in the conda environment. To avoid this, set the LD_LIBRARY_PATH to prioritize the conda environment’s libraries:

export LD_LIBRARY_PATH=/home/miniconda3/envs/R3.6/lib:$LD_LIBRARY_PATH

Then try reinstalling ncdf4 again:

install.packages("ncdf4")

5. Rebuild the ncdf4 Package in the Conda Environment

Rebuild the ncdf4 package within your conda environment to ensure it is correctly linked to the libraries provided by conda:

  1. Activate your conda environment:
    conda activate R3.6
    
  2. Reinstall ncdf4 in R:
    install.packages("ncdf4", type = "source")
    

6. Use Conda to Manage R and Libraries

If the conflict persists, you can consider installing both R and its libraries using conda, as this can avoid potential conflicts between system and conda libraries:

conda install -c conda-forge r-base r-ncdf4

This will ensure that everything (R, NetCDF, ncdf4) is managed under conda, which often simplifies dependency management.

7. Check for System and Conda Version Conflicts

If the issue persists, check the version of libffi and libp11-kit being used:

ldd /home/miniconda3/envs/R3.6/lib/R/library/ncdf4/libs/ncdf4.so

This command will show the shared libraries that ncdf4.so is trying to link to. Look for any discrepancies between system and conda versions.

If there are any conflicts, you can try forcing conda to rebuild the environment’s dependencies, or adjust the LD_LIBRARY_PATH so it prioritizes the conda environment’s libraries over system libraries.


If none of these solutions work, sharing the output of the ldd command above may help to better understand the source of the conflict.