72

I use an out-of-the-box Anaconda installation to work with Python. Now I have read that it is possible to also "include" the R world within this installation and to use the IR kernel within the Jupyter/Ipython notebook.

I found the command to install a number of famous R packages: conda install -c r r-essentials

My beginner's question:

How do I install R packages that are not included in the R-essential package? For example R packages that are available on CRAN. "pip" works only for PyPI Python packages, doesn't it?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Frank
  • 1,315
  • 1
  • 10
  • 14
  • If you install packages from inside of R via the regular `install.packages` (from CRAN mirrors), or `devtools::install_github` (from GitHub), they work fine. – alistaire Jan 26 '16 at 21:10
  • Thank you! Maybe that is the easiest way. I will add your comment to the answer. – Frank Jan 26 '16 at 21:25
  • @alistaire For the packages installed thru `install.packages` in R, do they need to be in the same directory with Anaconda? I've tried loading installed packages in R in Jupyter but doesn't work. Can you be more specific how you got it working? Thanks! – SeanM Feb 04 '16 at 21:50
  • @SeanM No, mine are completely unrelated to Anaconda aside from getting loaded within Jupyter. The default install location is within the R framework: `Library/Frameworks/R.framework/Versions/3.2/Resources/library`. `install.packages` gets that path by calling `.libPaths()`, which you can do yourself if you want to see where they are. Installing via R.app, RStudio, and an R Jupyter notebook all put them in that same place, which is where R goes to look for them—`library` also calls `.libPaths`. You can mess with `.libPaths()` if you like (see `?.libPaths`), but I wouldn't recommend it. – alistaire Feb 04 '16 at 22:37
  • @alistaire I am having issues with the `install.packages` approach. Briefly, it fails with `sh: symbol lookup error: sh: undefined symbol: rl_signal_event_hook`. Full details are in [this question](http://stackoverflow.com/questions/36652015/how-to-install-r-packages-not-in-the-conda-repositories). If you guys got this approach to work, I would greatly appreciate pointers on what I am doing wrong. – joelostblom Apr 15 '16 at 16:19

11 Answers11

41

Now I have found the documentation:

This is the documentation that explains how to generate R packages that are only available in the CRAN repository: https://www.continuum.io/content/conda-data-science

Go to the section "Building a conda R package".

(Hint: As long as the R package is available under anaconda.org use this resource. See here: https://www.continuum.io/blog/developer/jupyter-and-conda-r)

alistaire's answer is another possibility to add R packages:

If you install packages from inside of R via the regular install.packages (from CRAN mirrors), or devtools::install_github (from GitHub), they work fine. @alistaire

How to do this: Open your (independent) R installation, then run the following command:

install.packages("png", "/home/user/anaconda3/lib/R/library")

to add new package to the correct R library used by Jupyter, otherwise the package will be installed in /home/user/R/i686-pc-linux-gnu-library/3.2/png/libs mentioned in .libPaths() .

Ömer An
  • 600
  • 5
  • 16
Frank
  • 1,315
  • 1
  • 10
  • 14
  • You can also use `.libPaths` to set the path at which you want packages to be installed if you pass it an argument; see `?.libPaths`. – alistaire Apr 15 '16 at 18:06
  • You can also run `install.packages` in a Jupyter cell: `install.packages('package name', 'installation path (ending with Anaconda3\R\library\learningr)', repo='repo link. Check https://cran.r-project.org/mirrors.html')`. The `repo` is there since a repo needs to be specified when installing packages in Jupyter, otherwise it will throw a `trying to use CRAN without setting a mirror` error. – seismatica Dec 06 '17 at 11:28
  • 9
    The urls provided are dead – user3375672 Jul 29 '18 at 06:55
  • I found that `.libPaths()[2]` contains the path to `"~/.conda/envs//lib/R/library"`. So you can do `install.packages("png", .libPaths()[2])`. – mathause Jan 29 '19 at 17:22
26

To install other R Packages on Jupyter beyond R-essentials

install.packages('readr', repos='http://cran.us.r-project.org')

One issue is that the specific repository is the US.R-Project (as below). I tried others and it did not work.

N.B. Replace readr with any desired package name to install.

jkr
  • 17,119
  • 2
  • 42
  • 68
Yaw
  • 261
  • 3
  • 2
20

Here's a conda-centric answer. It builds on Frank's answer and the continuum website: https://www.continuum.io/content/conda-data-science with a bit more detail.

Some packages not available in r-essentials are still available on conda channels, in that case, it's simple:

conda config --add channels r
conda install r-readxl

If you need to build a package and install using conda:

conda skeleton cran r-xgboost
conda build r-xgboost
conda install --use-local r-xgboost

that last line is absent in the continuum website because they assume it gets published to anaconda repository first. Without it, nothing will be put in the envs/ directory and the package won't be accessible to commandline R or Jupyter.

On a mac, I found it important to install the Clang compiler for package builds:

conda install clangxx_oxs-64
Ziggy Eunicien
  • 2,858
  • 1
  • 23
  • 28
  • 1
    For me this answer worked only for some packages. For other packages I got an Error on the second step `conda build r-xgboost`. "make: /home/user/anaconda3/conda-bld/r-matrixstats_1516727877269/_h_env_placehold_pl/bin/x86_64-conda_cos6-linux-gnu-cc: Command not found make: *** [/home/user/anaconda3/conda-bld/r-matrixstats_1516727877269/_h_env_placehold_pl/lib/R/etc/Makeconf:160: 000.init.o] Error 127 ERROR: compilation failed for package ‘matrixStats’" – burton030 Jan 24 '18 at 08:31
  • @burton030 I seem to get the same error with you. Have you found any solution? – ytu Feb 25 '18 at 05:47
  • Hey, this answer worked for me, but I cannot install these packages into r environments with mro-base. Instead, it must be r-base. Do you know any way to build packages for mro-base or does this require something like ``conda skeleton mran r-mice`` which does currently not exist? – tobiasraabe Mar 12 '18 at 17:54
  • if ```conda install --use-local r-xgboost``` doesn't work, replace it by ```conda install -c ${CONDA_PREFIX}/conda-bld/ r-xgboost```. check [this](https://github.com/conda/conda/issues/7024) out!. – Manuel F May 03 '22 at 10:28
15

I found an easy workaround. I suppose that you have an RStudio IDE for you R. It is weird to use RStudio for that, but I tried straight from R in my terminal and it didn't work. So, in RStudio console, just do the usual adding the path to your anaconda directory (in OSX,'/Users/yourusernamehere/anaconda/lib/R/library')

So, for example,

install.packages('package','/Users/yourusernamehere/anaconda/lib/R/library')

I feel ashamed to post such a non-fancy answer, but that is the only one that worked for me.

Deninhos
  • 629
  • 6
  • 8
5

Adding it here so other beginners already working with Jupyter notebooks with Python and interested in using it with R: additional packages available for Anaconda can be installed via terminal using the same command used to instal the essential packages.

Install r-essentials

conda install -c r r-essentials

Install microbenchmark (infrastructure to accurately measure and compare the execution time of R expressions)

conda install -c r r-microbenchmark
A. Beal
  • 93
  • 1
  • 6
5

To install a CRAN package from the command line:

R --slave -e "install.packages('missing-package', repos='http://cran.us.r-project.org')"
Alf Eaton
  • 5,226
  • 4
  • 45
  • 50
5

Disclosure: Though not the case when I first answered this, I am now part of the Conda Forge R team. However, this is volunteer work - I receive no compensation for my work there nor whether you follow the advice in this answer.


Use Conda Forge

Five years out from the original question, I'd assert that a more contemporary solution would simply be: use Conda Forge. The Conda Forge channel not only provides broader coverage of CRAN, but also has a simple procedure and great turnaround time (typically under 24 hours) for adding a missing CRAN package to the channel.

Start from Conda Forge

I'd recommend using Conda Forge for the full stack, and use a dedicated environment for each R version you require.

conda create -n r41 -c conda-forge r-base=4.1 r-irkernel ...

where ... is whatever additional packages you require (like r-tidyverse). The r-irkernel package is optional, but included here because OP mentions using R in Jupyter.

If your environment with Jupyter (which should be in a separate environment) also has nb_conda_kernels installed, then this environment will automatically be discovered by Jupyter.

Install from Conda Forge

Generally, all R packages on CRAN have an r- prefixed to the package name on Conda Forge, and the package name is converted to lowercase. So, if your package of interest is PkgName, first try

conda install -n r41 -c conda-forge r-pkgname

If the package is not available, then proceed to either add it or request it.

Submit a CRAN package with Conda R Skeleton Helper

There is a helpful script collection, called conda_r_skeleton_helper for creating new Conda Forge recipes for CRAN packages. There are clear directions in the README.

In broad strokes, one will

  • clone the conda_r_skeleton_helper repository
  • edit the packages.txt file to include r-pkgname
  • run the script to generate the recipe
  • fork and clone the conda-forge/staged-recipes
  • copy the new recipe folder to the stage-recipes/recipes folder
  • commit changes, push to the fork, then submit a Pull Request back to Conda Forge

This takes maybe ~15 mins of work. Once submitted, most packages take under 24 hours to get accepted, feedstocked, and deployed to the Conda Forge channel. Once the feedstock is up and running, the Conda Forge infrastructure uses a bot to auto-detect version updates, generate new pull requests, and even auto-merge Pull Requests that successfully build. That is, maintainers have a very minimal workload, and if there are issues, a team is available to help out.

Automation

Personally, I have all this scripted (see scripts). I have a copy of these scripts in the root of a local copy of my staged-recipes fork. From a shell, I run:

sh new-cran-pkg.sh r-foo

and it:

  1. Synchronizes my staged-recipes main branch to upstream (conda-forge).
  2. Creates a new branch for r-foo.
  3. Runs the latest copy of conda_r_skeleton_helper for the package (here r-foo).
  4. Adds, commits, and pushes the recipe to my fork.

I then just have to visit GitHub to set up the Pull Request. Takes less than a minute.

Ask for help

If you have issues with submitting a recipe or updating an existing one, please ping me in a GitHub Issue or Pull Request (@mfansler).

File a Package Request

For users uncomfortable with creating and maintaining a Conda Forge build, packages can be requested on Conda Forge's staged-recipes repository by filing a new Issue. There is a template for Package Request, that includes some information fields to be filled in.

merv
  • 67,214
  • 13
  • 180
  • 245
3

I had a problem when trying to install package from github using install_github("user/package") in conda with r-essentials. Errors were multiple and not descriptive.

Was able to resolve a problem using these steps:

  • download and unzip the package locally
  • activate correct conda environment (if required)
  • run R from command line
  • library(devtools)
  • install('/path/to/unzipped-package')
  • Command failed due to missing dependancies, but now I know what's missing!
  • run install.packages('missing-package', repos='http://cran.us.r-project.org') for all dependancies
  • run install('/path/to/unzipped-package') again. Now it should work!
volodymyr
  • 7,256
  • 3
  • 42
  • 45
1

Someone suggested a not so elegant way around it, but actually it doesn't matter as long as it works fine.

install.packages('package','/Users/yourusernamehere/anaconda/lib/R/library')

I spent almost an entire morning looking for an answer to this problem. I was able to install the libraries on RStudio but not on Jupyter Notebook (they have different versions of R) The above solution "almost" worked, it's just that I found the Jupyter Notebook was trying to install in a different directory, and it will report what directory. So I only changed that and it worked as a charm... thanks to Dninhos

Hamza
  • 171
  • 1
  • 10
rojour
  • 11
  • 5
1

Install rpy2 with conda and add following line in your Jupyter notebook.

%load_ext rpy2.ipython

In next chunks, you can simply run any r code by specifying %R

Below is my favorite method to install and/or load r package

%R if (!require("pacman")) install.packages("pacman")
%R pacman::p_load(dplyr, data.table, package3, package4)

p_load argument will install + load the package if it's not in your lib else it will simply load it.

1

What worked for me is install.packages("package_name", type="binary"). None of the other answers have worked.

bixiou
  • 124
  • 2
  • 10