Carmen
Carmen

Reputation: 793

Can you install a Python package via R - Reticulate

I am about to create a python interface in R with the package Reticulate. In order to access the python functions in R, the respective python packages need to be installed.

Two questions came to my mind:

1) If you use the reticulate package, does the Anaconda package need to be installed? Or is it sufficient to install the python packages only?

2) Is it possible to install python packages in R, similar to install.packages("r_package")?

Does anyone have experience with this Topic? Thanks in advance!

Upvotes: 5

Views: 6550

Answers (3)

Edgar Manukyan
Edgar Manukyan

Reputation: 1301

In case you need a specific version of Python modules then place == after module name, e.g. the following will install specific versions of 3 modules using pip:

reticulate::conda_install(c("PyMuPDF==1.14.20", "PyPDF2==1.26.0", "reportlab==3.5.23"),
                            envname = "myenv", pip = TRUE)

Upvotes: 1

Dale Jacques
Dale Jacques

Reputation: 66

I'll add a little bit of nuance to the previous answer.

Like @f0nzie said, Anaconda is not a package, but a package manager. Ideally, you will create an environment using Anaconda to assist with your package management and version control. The documentation for conda environments is here.

Now, you can install python packages to your anaconda package in R. That is possible using reticulate::conda_install(envname, packages). The documentation for conda_install() can be found here.

Upvotes: 4

f0nzie
f0nzie

Reputation: 1205

1) The R package reticulate can work with the default python or with Anaconda2 or Anaconda3. If you want Anaconda to work with R, you will have to install Anaconda first. Once installed, you call the library(reticulate), and run py_config() or reticulate::py_discover_config(), that will give you the list of paths and environment used by the Python installation. Then, once you know the Python path, you add a line like this use_python("/opt/miniconda2/bin/python"), right after library(reticulate) and you are in business.

2) to install Python packages so R (or reticulate) can see them, you have to install them as regular Python packages from a terminal or console; not R. Example: conda install numpy to install numpy, or conda install scipy to install scipy, and so on.

I am just doing all this in a Docker container rocker/rstudio. It should be easier in a standard OS.

Here is step-by-step instructions: rstudio reticulate

Cheers!

Upvotes: 4

Related Questions