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

With Anaconda you can easily install the R programming language and over 80 of
the most used R packages for data science, and easily create and share your own
custom R packages.

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

As of early 2018, the default R interpreter installed into new environments is
MRO. You can specify the MRO interpreter with the ``mro-base`` package or the R
interpreter with the ``r-base`` package. Unless you request a change, conda
will continue to use the existing interpreter in each environment.

To run the commands below on Windows use Start - Anaconda Prompt. On macOS or
Linux open a Terminal.

MRO supported operating systems
===============================

* 64-bit systems only for all operating systems---Windows, macOS and Linux.
* Windows 7.0 SP1, Windows 8.1, Windows 10, Windows Server 2008 R2 SP1 and
  Windows Server 2012.
* Linux---CentOS, Red Hat Enterprise Linux, Debian and Ubuntu.

Anaconda with MRO is not currently supported on macOS but will soon be supported
on macOS El Capitan (10.11) and later.

Anaconda with R is supported on macOS Yosemite (10.10) and later.

Creating an environment with MRO and R Essentials
=================================================

#. `Download <https://www.anaconda.com/downloads>`_ and
   :doc:`install <../../install/index>` Anaconda.

* Create a new conda environment with MRO and all the r-essentials conda
  packages built from `MRAN <https://mran.microsoft.com/>`_::

    conda create -n mro_env r-essentials

* Activate the environment::

    conda activate mro_env

* List the packages in the environment::

    conda list

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

When using MRO conda packages, starting the R interactive interpreter shows
Microsoft R Open in the startup message.

Anaconda Navigator, the Anaconda graphical package manager and application
launcher, also creates MRO environments by default. You may instead select R
when creating a new conda environment from within Navigator.

Microsoft R Client
==================

`Microsoft R Client
<https://docs.microsoft.com/en-us/machine-learning-server/r-client/what-is-microsoft-r-client>`_
is a free, community-supported data science tool for high performance analytics
built on top of MRO. Additionally, R Client introduces the `powerful ScaleR
technology
<https://docs.microsoft.com/en-us/machine-learning-server/r/tutorial-revoscaler-data-import-transform>`_
and its proprietary functions to benefit from parallelization and remote
computing.

Microsoft R Client is now available as a conda package (``r-mrclient``). Users
of Windows or RHEL-7/CentOS7/Ubuntu 14.04 and above also have the option to
``conda install`` the `MicrosoftML R package for machine learning
<https://docs.microsoft.com/en-us/machine-learning-server/r-reference/microsoftml/microsoftml-package>`_
(``r-mrclient-mml``) and the `pre-trained models for sentiment analysis and
image detection
<https://docs.microsoft.com/en-us/machine-learning-server/install/microsoftml-install-pretrained-models>`_
(``r-mrclient-mlm``).

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

* Update all of the packages and their dependencies with one command::

    conda update r-essentials

* If a new version of a package is available in the R channel, you can use
  ``conda update`` to update specific packages.

Creating a new environment with R instead of MRO
================================================

When creating a new environment, you can use R and not MRO by explicitly
including ``r-base`` in your list of packages. This option will continue to be
supported for users who prefer R or use platforms that do not support MRO,
including 32-bit operating systems and older versions of macOS.

With conda 4.4::

      conda create -n r-environment r-essentials r-base
      conda activate r-environment

Switch an environment from R to MRO
===================================

* Activate the environment containing R.

* If you use conda 4.4 or later, run::

      conda install mro-base

* If you use conda 4.3, run::

      conda remove --force r-base _r-mutex
      conda install mro-base

The environment's R interpreter will switch from R to MRO.

Switch the default R interpreter from MRO to R
==============================================

* Run ``conda info`` and check your version of conda.

* If your version of conda is below 4.4, run ``conda update conda`` to update
  conda to the latest version.

* Run::

       conda config --system --set pinned_packages _r-mutex=*=anacondar*

The default R interpreter will switch from MRO to R.

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

Creating and sharing custom R bundles is similar to creating and sharing conda
packages.

EXAMPLE: Create a simple custom R bundle metapackage named "Custom-R-Bundle"
that contains several popular programs and their dependencies::

   conda metapackage custom-r-bundle 0.1.0 --dependencies r-irkernel jupyter r-ggplot2 r-dplyr --summary "My custom R bundle"

Share the new metapackage by uploading it to your channel on `Anaconda Cloud
<https://anaconda.org>`_::

  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::

  conda install -c <your anaconda.org username> custom-r-bundle

For more information, see `Jupyter and conda for R language
<https://www.anaconda.com/developer-blog/jupyter-and-conda-r/>`_.

Mirroring the R channel
=======================

Many Anaconda Enterprise customers maintain a local mirror of the R channel.

When mirroring the R channel for the first time after the early 2018 update,
clean the existing packages by running the command
``anaconda-server-sync-conda`` with the option ``--clean``.

R packages
==========

* ``r-essentials``: The R Essentials bundle contains the IRKernel and more than
  80 of the most popular R packages for data science, including dplyr, shiny,
  ggplot2, tidyr, caret and nnet.

* ``mro-basics``: The MRO Basics metapackage contains everything in the
  Microsoft MRO installers. It does not include ``r-mrclient``,
  ``r-mrclient-mml``, or ``r-mrclient-mlm``.

* ``r-mrclient``: Microsoft R Client is a free, community-supported, data
  science tool for high performance analytics.

* ``r-mrclient-mml``: MicrosoftML provides state-of-the-art fast, scalable
  machine learning algorithms and transforms for R. Depends on ``r-mrclient``.

* ``r-mrclient-mlm``: MicrosoftML Machine Learning Models are pre-trained
  machine learning models for sentiment analysis and image detection. Depends on
  ``r-mrclient-mml``.

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

To uninstall the R Essentials package, run::

    conda remove r-essentials

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

User group
==========

For community help on using conda with MRO, join the `conda
<https://groups.google.com/a/anaconda.com/forum/#!forum/conda>`_ email group.

Resources
=========

Here are our more popular resources on using Anaconda with the R programming
language:

* :doc:`R Language packages available for use with Anaconda
  <../../packages/r-language-pkg-docs>`--There are hundreds of R language
  packages now available, and several ways to get them.

* :doc:`Navigator tutorial <../../navigator/tutorials/index>`--Use the R
  programming language with Anaconda Navigator. The Anaconda Navigator
  graphical interface (GUI) makes it easy for even new users to use and run the
  R language in a Jupyter Notebook.

* :doc:`Using R packages with Anaconda and Cloudera CDH
  <../../../anaconda-scale/cloudera-cdh>`--Anaconda Scale provides resource
  management tools to easily deploy Anaconda across a cluster. It helps you
  manage multiple conda environments and packages, including Python and R
  language, on bare-metal or cloud-based clusters.

* `Blog post: Jupyter and conda for R
  <https://www.anaconda.com/blog/developer/jupyter-and-conda-r>`_--The many
  benefits that Jupyter, the IRKernel and conda can provide for data scientists
  working with the R programming language.

* `Blog post: Anaconda for R users: SparkR and rBokeh
  <https://www.anaconda.com/blog/developer-blog/anaconda-r-users-sparkr-and-rbokeh>`_--
  Data Scientist Christine Doig presents two projects for the R programming
  language that are powered by Anaconda. rBokeh allows you to create beautiful
  interactive visualizations. Scale your predictive models with SparkR through
  Anaconda's cluster management capabilities.

* `Using Anaconda with Hadoop: Distributed language processing with PySpark
  <https://anaconda.org/anaconda-cluster/notebook-pyspark-language/notebook>`_--
  This notebook example shows how Anaconda for cluster management makes it easy
  to manage packages, including Python and R, on a Hadoop cluster with PySpark.

* `Webinar: Predict. Share. Deploy.
  <http://go.continuum.io/predict-share-deploy/>`_--Download the webinar video
  to:

  * Build predictive models in Python with Anaconda using Python packages such
    as pandas and scikit-learn in Jupyter Notebooks.

  * Use modern open data science languages including Python and R together in
    your analysis.

  * Share your results with your entire data science team.

* `Webinar: Anaconda for R Users
  <https://speakerdeck.com/chdoig/anaconda-for-r-users>`_--Download the slides
  from the webinar to see how Anaconda makes package, dependency and
  environment management easy with R language and other Open Data Science
  languages.
