How to install R packages that are not available in "R-essentials"?

85,832

Solution 1

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() .

Solution 2

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.

Solution 3

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

Solution 4

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.

Solution 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
Share:
85,832
Frank
Author by

Frank

Updated on July 05, 2022

Comments

  • Frank
    Frank almost 2 years

    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?

  • alistaire
    alistaire about 8 years
    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.
  • captain_M
    captain_M almost 7 years
    I'm surprised this solution worked for me, but it really was that simple.
  • Collective Action
    Collective Action over 6 years
    I tried this and I am still getting a non-zero exit status error.
  • seismatica
    seismatica over 6 years
    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.
  • burton030
    burton030 over 6 years
    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/b‌​in/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:1‌​60: 000.init.o] Error 127 ERROR: compilation failed for package ‘matrixStats’"
  • ytu
    ytu about 6 years
    @burton030 I seem to get the same error with you. Have you found any solution?
  • tobiasraabe
    tobiasraabe about 6 years
    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?
  • user3375672
    user3375672 almost 6 years
    The urls provided are dead
  • mathause
    mathause over 5 years
    I found that .libPaths()[2] contains the path to "~/.conda/envs/<ENV_NAME>/lib/R/library". So you can do install.packages("png", .libPaths()[2]).
  • Adam Kuzański
    Adam Kuzański over 2 years
    Props! It works and it is the easiest solution :)
  • Manuel F
    Manuel F about 2 years
    if conda install --use-local r-xgboost doesn't work, replace it by conda install -c ${CONDA_PREFIX}/conda-bld/ r-xgboost. check this out!.