Python from RMarkdown – Matplotlib problems

Tags: , , ,

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)
use_condaenv('Toy_MPL') # this environment contain matplotlib and produces the error
#use_condaenv('Toy') # this environment does not contain matplotlib and no error

# this works regardless of which environment I use
pysys <- import('sys')

[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
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 of use_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 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


In my original question, I referred to this question in which there was a suggestion to downgrade matplotlib to version 3.2.0 because reticulate was not up to date with changes in matplotlib. I followed up further on that suggestion and have found a resolution (for now).


Removing pip and conda installed versions of matplotlib, and then installation of matplotlib version 3.2.2 with conda (NOT pip) resolves the problem. Installing matplotlib with pip leads to other errors.


In the response to the other question, the suggestion was to do:

pip install matplotlib==3.2  

I tried this and ended up with other errors that I also could not track down. So, I uninstalled matplotlib and then reinstalled it with

pip install matplotlib==3.3.1

in hopes of getting back to where I was. This also did not work and the new errors persisted. I then removed matplotlib completely and with pip and reinstalled version 3.3.1 with conda:

pip uninstall matplolib
conda install matplotlib

This got me back to matplotlib version 3.3.1 and the original error I mention in my question. I then tried installing matplotlib version 3.2 with conda:

conda install matplotplib==3.2

The installed version is 3.2.2 and not 3.2.0, as suggested in the response, but when I did this, the original problem seems to be resolved.

There is clearly a difference in dependency resolution between pip and conda in this case, and conda provides a version of matplotlib that plays nicely with reticulate. I do not at this point know what the difference is, however.

Source: stackoverflow