Search code examples
pythonrpipr-markdown

How to `pip install` Python module in RStudio?


Hoping for some help. How does one pip install a Python package for reticulating from within an RStudio R-Markdown (.Rmd) file?

For example, the lasio Python package is not available through conda. Hence, the following .Rmd code was not able to add the package to my environment:

```{r}
library(reticulate)
py_install("lasio")    # did not work
```

I also tried to install the package with Python code (to no avail):

```{python}
import sys
!{sys.executable} -m install lasio    # did not work

$ python -m pip install lasio    # also did not work
```

Turns out that the lasio package could be installed/added to my environment by typing pip install lasio in RStudio's Terminal window/tab. However, I'm hoping for code that can be run with .R or .Rmd code so as to automate the process when ported to another machine. Thanks.


Solution

  • py_install() has a pip argument that you can set to TRUE which should use pip to install your module:

    py_install("lasio",pip=TRUE)
    

    Alternatively, you can use system2() to pass calls directly into the terminal.

    system2("pip install lasio")