rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.67k stars 327 forks source link

knitr-engine with matplotlib (conda environment) #434

Open masonlr opened 5 years ago

masonlr commented 5 years ago

Does the reticulate knitr-engine support matplotlib figures when using pdf_document as an output format?

Example

With the following minimal content (in R Markdown format),

---
title: "Knit matplotlib via reticulate"
output: pdf_document
---

​```{python}
import matplotlib.pyplot as plt
plt.figure()
plt.plot([1, 2, 3])
​```

the plot renders correctly inline when using "Run Selected Line(s)" in the RStudio editor. When using "Knit", however, I'm getting the following error:

processing file: test.Rmd
  |................................                                 |  50%
  ordinary text without R code

  |.................................................................| 100%
label: unnamed-chunk-1 (with options) 
List of 1
 $ engine: chr "python"

2019-02-20 10:01:52.546 R[58967:533493] -[NSApplication _setup:]: unrecognized selector sent to instance 0x7f9639702bc0
Quitting from lines 7-10 (test.Rmd) 
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  c++ exception (unknown reason)
Calls: <Anonymous> ... py_capture_output -> force -> <Anonymous> -> py_call_impl
Execution halted

Environment

The R package versions are:

The python environment was set up using:

conda create -n test
source activate test
conda install matplotlib

The reticulate python environment variable was set using a local .Rprofile file using:

Sys.setenv(RETICULATE_PYTHON = "/Users/username/miniconda3/envs/test/bin/python")

Attempts to solve

  1. I have tested the reticulate::use_condaenv() method via an .Rprofile file using:
library(reticulate)
use_condaenv(condaenv = "test", conda = "/Users/username/miniconda3/bin/conda")
  1. I have also tested using conda versions of R, knitr, reticulate (linked using export RSTUDIO_WHICH_R=/Users/username/miniconda3/envs/test/bin/R) installed via:
conda install r-essentials reticulate
zkurtz commented 5 years ago

Getting a comparable error in the context of a virtualenv with output: github_document

tosi-n commented 5 years ago

Having similar problems too but as stated above with a virtual environment trying to knit to a github doc. Please any suggestions would be very helpful.

Got the below error message:

Quitting from lines 51-65 (python2.rmd) Error in py_call_impl(callable, dots$args, dots$keywords) : RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. See the Python documentation for more information on installing Python as a framework on Mac OS X. Please either reinstall Python as a framework, or try one of the other backends. If you are using (Ana)Conda please install python.app and replace the use of 'python' with 'pythonw'. See 'Working with Matplotlib on OSX' in the Matplotlib FAQ for more information.

Detailed traceback: File "", line 1, in File "/Users/lade/Dev/PyDsc/env/lib/python3.6/site-packages/seaborn/init.py", line 6, in from .rcmod import * File "/Users/lade/Dev/PyDsc/env/lib/python3.6/site-packages/seaborn/rcmod.py", line 5, in from . import palettes, _orig_rc_params File "/Users/lade/Dev/PyDsc/env/lib/python3.6/site-packages/seaborn/palettes.py", line 12, in from .utils import desatur Calls: ... py_capture_output -> force -> -> py_call_impl -> .Call Execution halted

etabeta78 commented 5 years ago

Just to add that this issue is not related specifically to conda environment. Using OSX system Python you get the same error.

It is enough to create a blank Rmd, to call in an R chunk

Sys.setenv(RETICULATE_PYTHON = '/Library/Frameworks/Python.framework/Versions/3.6/bin/python3')

and then add a python chunk

import matplotlib.pyplot as plt

to get a file that executes fine when run in RStudio and gives the same error reported by tosi-n:

Error in py_call_impl(callable, dots$args, dots$keywords) :
RuntimeError: Python is not installed as a framework. The Mac OS X backend will not be able to function correctly if Python is not installed as a framework. 

Importing just matplotlib does not trigger this error, but since RStudio can easily run such a chunk I would expect the knitr-engine shall be able to as well ;-)

kevinushey commented 5 years ago

https://github.com/matplotlib/matplotlib/blob/master/doc/users/prev_whats_new/whats_new_3.1.0.rst#matplotlib-no-longer-requires-framework-app-build-on-macosx-backend suggests that this is fixed in the newest versions of matplotlib.

etabeta78 commented 4 years ago

Sorry for the late reply, but I can confirm that latest matplotlib fixes the issue I was experiencing with base Python 3.

I still find it weird that running chunks in RStudio was having no problem and knitr-engine was failing, but in the end I will just use it happily without further questions :-)