Can you install a Python package via R - Reticulate
Asked Answered
P

3

5

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!

Periphrasis answered 15/6, 2017 at 12:24 Comment(3)
1. "Anaconda" isn't a package. Its a bundle of packages and other stuff. They may or may not exist on any given python installation. 2. Are you just asking how to install python packages? Like, using pip?Anamorphoscope
1. I am wondering if I can install a single package, e.g. XLWings and then use it in R via Reticulate without having Anaconda installed on my Computer 2. Installing a package in R only requires "install.packages("r-package"). In my case I would like to install xlwings, but not via pip install xlwings, but directly in R. Is that possible?Periphrasis
If you have the pip module in the python you are calling from R, you can use that to install python modules.Anamorphoscope
C
4

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.

Chemnitz answered 5/12, 2018 at 20:10 Comment(0)
C
4

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!

Curium answered 20/9, 2017 at 2:14 Comment(0)
C
4

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.

Chemnitz answered 5/12, 2018 at 20:10 Comment(0)
I
1

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)
Infidelity answered 30/8, 2019 at 4:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.