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?
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() .
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.
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
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.
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
To install a CRAN package from the command line:
R --slave -e "install.packages('missing-package', repos='http://cran.us.r-project.org')"
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!
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 a r- prefix to the package name on Conda Forge. 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.
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.
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.
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
What worked for me is install.packages("package_name", type="binary").
None of the other answers have worked.
Related
I have some issues with a published package and wish to edit the code myself (may generate a pull request later to contribute). I am quite confused about how to do this since it seems there is a lack of step-by-step guidance. Could anybody give me a very detailed instruction about how this is done (or a link)? My understanding and also my questions about the workflow are:
Fork the package through git/github and have a local synced copy (done!).
Create a new Anaconda environment (done!)?
Install the package as normal: $conda install xxx or $python setup.py develop?
Do I make changes to the package directly in the package folder in Anaconda if I use python setup.py develop?
Or make changes to the local forked copy and install/update again and what are the commands for this?
Do I need to update the setup.py file as well before running it either way?
You can simply git-clone the package repo to your local computer and then install it in "development" or "editable" mode. This way you can easily make changes to the code while at the same time incorporating it into your own projects. Of course, this will also allow you to create pull requests later on.
Using Anaconda (or Miniconda) you have 2 equivalent options for this:
using conda (conda-develop):
conda develop <path_to_local_repo>
using pip (pip install options)
pip install --editable <path_to_local_repo>
What these commands basically do is creating a link to the local repo-folder inside the environments site-packages folder.
Note that for "editable" pip installs you need a a basic setup.py:
import setuptools
setuptools.setup(name=<anything>)
On the other hand the conda develop <path_to_local_repo> command unfortunately doesn't work in environment.yml files.
I followed the instructions here: Can't find package on Anaconda Navigator. What to do next?
I clicked Open terminal from environment on Anaconda navigator, and then used "pip3 install lmfit" in the terminal. But after installing the lmfit package using pip3, I still cannot find it in the conda list. What should I do?
The Problem
At the time of this question, Conda builds of pip had only just started including a pip3 entrypoint,1 therefore pip3 is very likely referring to a non-Conda version of Python and that is where the package was installed. Try checking which pip3 to find out where it went.
Recommendation
Conda First
Generally, it is preferable to use Conda to install packages in Conda environments, and in this case the package is available via the Conda Forge channel:
conda install -c conda-forge lmfit
Contrary to M. Newville's answer, this recommendation to prefer Conda packages is not about benefiting Conda developers, but instead a rule of thumb to help users avoid creating unstable or unreproducible environments. More info about the risks of mixing pip install and conda install can be found in the post "Using Pip in a Conda Environment".
Nevertheless, the comment that not all packages (and specifically lmfit) are found in the default repository and this complicates installation by requiring resorting to third-party channels is a good point. In fact, because third-parties are free to use different build stacks there are known problems with mixing packages built by Anaconda and those from Conda Forge. However, these issues tend to be rare and limited to compiled code. Additionally, adding trusted channels to a configuration and setting channel priorities heuristically solves the issue.
As for risks in using third-party channels, arbitrary Anaconda Cloud user channels are risky: one should only source packages from channels you trust (just like anything else one installs). Conda Forge in particular is well-reputed and all feedstocks are freely available on GitHub. Moreover, many Python package builds on Conda Forge are simply wrappers around the PyPI build of the package.
PyPI Last
Sometimes it isn't possible to avoid using PyPI. When one must resort to installing from PyPI, it is better practice to use the pip entrypoint from an activate environment, rather than pip3, since only some Conda builds of pip include pip3. For example,
conda activate my_env
pip install lmfit
Again, following the recommendations in "Using Pip in a Conda Environment", one should operate under the assumption that any subsequent calls to conda (install|upgrade|remove) in the environment could have undefined behavior.
PyPI Only
For the sake of completeness, I will note that a stable way of using Conda that is consistent with the recommendations is to limit Conda to the role of environment creation and use pip for all package installation.
This strategy is perhaps the least burden on the Python-only user, who doesn't want to deal with things like finding the Conda-equivalent package name or searching non-default channels. However, its applicability seems limited to Python-only environments, since other libraries may still need to resort to conda install.
[1]: Conda Forge and Anaconda started consistently including pip3 entrypoints for the pip module after version 20.2.
Installing a pure Python package, such as lmfit with the correct version of pip install lmfit should be fine.
Conda first is recommended to make the life of the conda maintainers and packagers easier, not the user's life. FWIW, I maintain both kinds of packages,
and there is no reason to recommend conda install lmfit over pip install lmfit.
In fact, lmfit is not in the default anaconda repository so that installing it requires going to a third-party conda channel such as conda-forge. That adds complexity and risk to your conda environment.
Really, pip install lmfit should be fine.
I need to use the Graph API to pull some data from Facebook and I'm using Conda for my package management. However, when I try to install it from Conda it gives me an error message saying:
PackagesNotFoundError: The following packages are not available from the current channels:
- facebook-sdk
Searching on Google sent me to a Conda package which I used but the version of the API on that is very old and the link is from 2011.
Can someone tell me how to install the latest version of the Graph API using Conda? I'm able to get it to install from PyPI install just fine.
There really isn't a reliable Anaconda Cloud channel to get the Facebook SDK for Python (which itself is a third-party open-source project). Instead, just follow the recommended installation from the package documentation, but make sure to activate your environment first. Also, install the prerequisites (looks like it only needs requests) through Conda first.
conda activate myenv
conda install requests
pip install -e git+https://github.com/mobolic/facebook-sdk.git#egg=facebook-sdk
Just be aware that, even though it is supported, installing things from PyPI into a Conda env can be lead to an unstable env (see "Using Pip in a Conda Environment"). I'd highly encourage you to create a separate env for this project (e.g., conda create -n fbenv python=3.7 requests).
Whenever I want to install a python package, I find the pip install <package> instructions on most of the sites / README.md documentation in github etc.
A colleague told me recently to try first a conda install <package> and if this fails (because the package is not available) to use the pip install process afterwards.
Is the try with the conda installation step really necessary / beneficial or can I just do the pip install directly?
It depends on your use case. Conda does more than pip does. Conda was developed after pip because the Conda folks did not think pip did enough. It aims to handle library dependencies outside of the realm of python such as C libraries, R packages, or really anything with a wheel. as well as handling the python packages themselves. This is important because these packages do not have the standard setup.py in their source code, so python will not install them into the site-packages directory which is useful for easy importing.
It is important to note that you can't use pip and conda interchangeably, as conda has a different packaging format.
To answer your question succinctly: If you use one, I would stick to it for the entirety of whatever you are doing, and not use conda "until it doesn't work for something" and then just switch to pip for installations that conda can't handle. That is a super good way to get into trouble you can't explain.
My advice: if you are sticking to python and python only, use pip. If you are considering outside libraries that have value to your project, conda is a good option.
I've been looking everywhere and cannot find a robust explanation.
I'm brand new to Python, coming from R. I had no issues installing packages there but I'm finding it to be rather confusing in Python.
So, I'm using Anaconda and I want to install this package into Python. It mentions using the conda install -c https://conda.anaconda.org/amueller wordcloud command but I have no idea where I'm supposed to run it.
Any advice would be much appreciated.
If you can't find a package with a simple conda search from the command line, run a search on the Anaconda website.
In your case, you'll find that the contributor amueller has his own channel and the package wordcloud is available.
Just run conda install -c amueller wordcloud=1.2.1 to install it.
You might want to create a separate environment using conda create first.
Run it from command line. You can directly install using pip as mentioned in the link. Don't forget to install the pre-requisite packages.
pip install wordcloud
If you are using windows, make sure your environmental path is set so that you can use the pip command directly from windows command prompt. Usually the environmental variable is updated when you install Anaconda distribution.
Conda is just another command which you can use to install packages. Procedure is exactly the same.
You may install packages from interface
Interface