I'm trying to use reticulate
to run some simple Python code in an RMarkdown document. I've found that if Matplotlib is in the conda environment, I get errors when trying to run a python code chunk, but I can run Python from R directly. Here's a simple example of what I see:
---
title: "Reticulate Test"
date: "9/21/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
use_condaenv('Toy_MPL') # this environment contain matplotlib and produces the error
#use_condaenv('Toy') # this environment does not contain matplotlib and no error
```
```{r}
# this works regardless of which environment I use
pysys <- import('sys')
pysys$version
```
[1] "3.8.5 (default, Sep 4 2020, 02:23:17) \n[Clang 10.0.0 ]"
```{python, engine.path = '/opt/miniconda2/envs/Toy_MPL/bin/python'}
# if Toy_MPL conda environment is used, the error is generated
# if Toy conda environment is used, I get the same output as above
import sys
print(sys.version)
```
Error in py_call_impl(callable, dots$args, dots$keywords) :
TypeError: use() got an unexpected keyword argument 'warn'
My first thought was that reticulate
was not seeing the various system libraries that are installed in the conda environment lib/
folder when Matplotlib is installed - there are a LOT of dependencies that come along with Matplotlib. I tried the following, but none worked:
- Set LD_LIBRARY_PATH in .Renviron to point to the correct library path.
- Call
use_python()
in addition to or instead ofuse_condaenv()
- set
engine.path
in the Python code chunk - I tried downgrading matplotlib to v3.2 (suggested here), but that caused a new set of errors:
Error in if (has_compatible_arch && has_preferred_numpy) valid_python_versions <- c(valid_python_versions, : missing value where TRUE/FALSE needed
- Checking NumPy, I see I have v1.19.1 (other errors suggest needing >1.6). And, reinstalling matplotlib v3.3.1 does not prevent the error. After this "fix" I end up having to rebuild the entire environment.
traceback()
gives me a CPP stack trace from the reticulate.so which is not interpretable.
My interpretation is that the environment created for RMarkdown does not point to the correct library locations, but I cannot determine how to set it correctly.
System info:
- Mac OS Catalina 10.15.6
- RStudio v1.3.1073
- reticulate v1.16
- conda v4.8.4
- Python in conda environments v3.8.5
- Matplotlib in Toy_MPL environment v3.3.1