-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rendering Rmd with matplotlib with reticulate in IDE seems to required tcl tk #13840
Comments
I've encountered the same problem while rendering Quarto document with both r and python cells in VSCode. |
Same issue here. Did you find any solution so far? |
I managed to find a very manual solution to this issue (thanks in part to this thread) In my case, I was trying to render a Quarto document with both R and Python cells in R Studio and hitting the exact same error. It seems that for whatever reason, the process to find the Tcl folder doesn't look in the correct place of where it is installed outside of your virtual environment, and so rendering returns the error you saw that it The trick is to find the In your case (according to your error message), the first place the In case you don't know where your Tcl folder is located, I managed to find mine by trying to install This returned: Hopefully this issue is fixed soon so we can avoid this process! |
Thanks for sharing workaround ! I know where my tcl8.6 is in the python installation - and settign TCL_LIBRARY env var to it, will indeed help make this works (no need to copy directory in that case) ---
title: "matplotlib test"
output: html_document
---
```{r}
# Using tcl available in the Python installation (outside virtual env)
Sys.setenv(TCL_LIBRARY = file.path(dirname(system("pyenv which python", intern = TRUE)), "tcl", "tcl8.6"))
```
```{r}
#reticulate::virtualenv_create("test")
#reticulate::virtualenv_install("test", "matplotlib")
reticulate::use_virtualenv("test")
```
```{python}
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.show()
``` Problably adjusting directory is long term solution for this virtual env Though, I believe the IDE should try to use AGG like reticulate packages does. I don't yet understand why the difference between rendering in IDE, and rendering using R console... 🤷 |
Interestingly, the tcl issue does not arise on Mac ("arm64"), but it does on Windows ("x86-64")... If you work with
I did not use Note that I use
Feel free to check out an example project where I use this here. |
I'm trying to test this out now, and I see that
And that seems to indicate
Note that the RStudio hook that tries to force matplotlib to use "agg" requires the rstudio/src/cpp/session/modules/SessionReticulate.R Lines 178 to 184 in 10551e8
@cderv if you're still hitting this could we take some time to debug together? |
This is still happening for me and This is with 2024.04.0-daily+581 on Windows We can definitely look into debugging this together This is only with Knit button and rendering happens in render pane. Using If I look at where this tkAgg is from I am finding this rstudio/src/cpp/session/modules/SessionReticulate.R Lines 113 to 117 in 10551e8
So the envar is set, if unset. and this is not overridden by agg it seems 🤔 |
Ah, for some reason I missed that this issue occurs specifically when using the Knit button. I tested when executing code directly in the IDE, and that's where we more forcefully set the backend to agg. I wonder if the code in reticulate above is wrong? It looks like the environment used to configure a default backend with matplotlib is normally called https://matplotlib.org/stable/users/explain/figure/backends.html If so, that would imply the fix should happen in reticulate... |
Which code of reticulate are you referring to ?
It seems indeed that this is not working, and the
I does get the same value in Python directly
Following hint at https://matplotlib.org/stable/users/explain/figure/backends.html#selecting-a-backend I see
So I believe I get rstudio/src/cpp/session/modules/SessionReticulate.R Lines 186 to 198 in 10551e8
While when using reticulate inside RStudio it seems to work at anyhow, happy to go live with you to try debug further |
Maybe the code block at https://github.com/rstudio/reticulate/blob/5d0a9a52286a173ac8868c1d548b8715247867ae/R/knitr-engine.R#L484-L501 needs to be run more aggressively? Maybe force its use in non-interactive sessions? I can't think of a reason why a user would want a non-agg backend in that case... We could have an environment variable like |
I do think it is not run in background session in fact.
So when doing Knit button, it runs render in background, and it doesn't apply the agg setting. Running in console this works
but in background is does not change tkAgg to agg
and that would be why it doesn't change. I would have thought the rstudio setting would apply rstudio/src/cpp/session/modules/SessionReticulate.R Lines 176 to 198 in 10551e8
but the hook does not apply it seems. So yes applying more aggressively seems interesting - or just using another way to test that we are running from RStudio
Yes I agree. |
QA note: testing requires the development version of reticulate, so please do:
and restart the R session before attempting to verify. |
Verified in RStudio Desktop 2024.04.0-daily+668on Windows 11. Checked both knitting and rendering from the console. In both cases, the plots display correctly, and |
System details
Steps to reproduce the problem
This is a simple Rmarkdown file
I used a new virtual environment to try isolate the issue.
rmarkdown::render()
in R console and there is no issueOpening to share the error and see if this is really a IDE issue or not (as explained below, no problem when rendering on its own)
Describe the problem in detail
This is the issue
It seems the tk backend is tried to be used. I even change Graphics settings to use AGG. But it does not work.
reticulate is supposed to set the agg backend when inside RStudio desktop
https://github.com/rstudio/reticulate/blob/5d0a9a52286a173ac8868c1d548b8715247867ae/R/knitr-engine.R#L484-L501
Probably why no error when render is called in R console
Adding the backend switch in the chunk makes the knit button works with no problem
Describe the behavior you expected
I would expect knitting in IDE works the same as calling
rmarkdown::render()
with no error. Especially as reticulate seems to do the work of setting the backend.I am sure this worked before, so maybe this is something that changed in recent version or related to some settings.
The text was updated successfully, but these errors were encountered: