Create an R environment using Mamba (a faster alternative to Conda)

Author

Nguyễn Ngọc Bình

  1. Install Mamba: If you haven’t already installed Mamba, you can do so by running the following command in your terminal or command prompt:

    conda install mamba -c conda-forge
  2. Create a YAML File: Create a YAML file (e.g., r_environment.yml) to specify the packages and dependencies for your R environment. Here’s an example YAML file:

    name: r_env
    channels:
      - r
      - conda-forge
    dependencies:
      - r=4.1
      - r-ggplot2
      - r-dplyr
      - r-tidymodels
      - r-mlr3
      - r-lightgbm
      - r-pROC
      # Add more R packages as needed

    In this example, we’ve included R version 4.1 along with several R packages such as ggplot2, dplyr, tidymodels, mlr3, lightgbm, and pROC. Adjust the list of packages to match your specific requirements.

  3. Create the Environment: To create the R environment using Mamba and the YAML file, run the following command:

    mamba env create -f r_environment.yml

    This will create a new Mamba environment named r_env and install the specified R packages and their dependencies.

  4. Activate the Environment: Activate the newly created environment with the following command:

    conda activate r_env

    Now, you’re working within the r_env environment.

  5. Install Additional R Packages: You can install additional R packages within the Mamba environment using the R console, just like you would in a regular R environment. For example:

    install.packages("new_package")
  6. Deactivate the Environment: When you’re finished working in the R environment, deactivate it by running:

    conda deactivate

    This will return you to the base Conda environment or system environment.

By following these steps, you can create and manage an R environment using Mamba, which offers improved speed and efficiency compared to Conda for package and environment management.