==============================
Using R language with Anaconda
==============================

With Anaconda (or Miniconda), you can install the R programming language and over 6,000 commonly used R packages for data science. You can also create and share your own custom R packages.

.. note::
   When using conda to install R packages, add ``r-`` before the regular package name. For instance, to install rbokeh, use ``conda install r-rbokeh``. To install rJava, use ``conda install r-rjava``.

The R Essentials bundle contains approximately 200 of the most popular R packages for data science, including the IRKernel, dplyr, shiny, ggplot2, tidyr, caret, and nnet.

The version of the R interpreter installed into your R environments is based on the version of the ``r-base`` package.

.. note::
   
   Run the commands in the following sections in Anaconda Prompt (Terminal on macOS/Linux).

Updating R packages
===================

.. caution::

   Exercise caution when using conda to update RStudio or other R packages to their latest versions. This might break your conda RStudio environment.

* Update all of the packages and their dependencies by running the following command::

    conda update r-caret

* If a new version of a package is available in the R channel, update specific packages by running the following command::

    conda update

Creating and sharing custom R bundles
=====================================

Creating and sharing custom R bundles is similar to creating and sharing conda packages. In the following example, we will create a simple custom R bundle metapackage named "Custom-R-Bundle".

#. Create the metapackage "Custom-R-Bundle" that contains several popular programs and their dependencies by running the following command:

   .. code-block:: none
      
      conda metapackage custom-r-bundle 0.1.0 --dependencies r-irkernel jupyter r-ggplot2 r-dplyr --summary "Custom-R-Bundle"

#. Upload the new metapackage to your channel on `anaconda.org <https://anaconda.org>`_ by running the following commands:

   .. code-block:: none
      
      conda install anaconda-client
      anaconda login
      anaconda upload custom-r-bundle-0.1.0-0.tar.bz2

Anyone can now access your custom R bundle from any computer by running the following command::

  # Replace <USERNAME> with your anaconda.org username
  conda install --channel <USERNAME> custom-r-bundle

Creating an environment with R
==============================

#. `Download and install Anaconda <https://docs.anaconda.com/anaconda/install/>`_.

#. Create a new conda environment with all the ``r-essentials`` conda packages built from CRAN by running the following command:

   .. code-block:: none
      
      conda create --name r_env r-essentials r-base

#. Activate the environment by running the following command:

   .. code-block:: none
      
      conda activate r_env

#. List the packages in the environment by running the following command:
   
   .. code-block:: none
      
      conda list

The list shows that the package ``r-base`` is installed and ``r-`` is listed in the build string of the other R packages in the environment.

Creating a new environment with R
=================================

When creating a new environment, you can use R by explicitly including ``r-base`` in your list of packages:

   .. code-block:: none

      # Replace <ENV_NAME> with a name for your R environment
      conda create --name <ENV_NAME> r-base r-essentials
      conda activate <ENV_NAME>

Uninstalling R Essentials
=========================

Uninstall the R Essentials package by running the following command::
   
   # Replace <ENV_NAME> with the name of the R environment
   conda activate <ENV_NAME>
   conda remove r-essentials

.. note::

   This removes only R Essentials and disables R language support. Other R language packages are not removed.

Resources
=========

Hundreds of :doc:`R Language packages are available for use with Anaconda <../reference/r-language-pkg-docs>` with several ways to get them.