tl:dr Rmarkdown crashes when loading ggplot2 library in combination with using python chunks in Rstudio 1.4.1717
My Rmarkdown file is crashing after I upgraded to 1.4.something. It's the same ggplot2 crash people are talking about in other threads (e.g. ggplot Crashing system, GGplot crashes Rstudio after upgrade or suddenly geplot crashes). The fix to upgrade to 1.4.1717 does not resolve the issue for me.
If I use library(ggplot2) in the console it works, in a script or new markdown file it works. However, combined with using reticulate it crashes. Here's chunk 1
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
library(dygraphs)
library(xts)
library(ggplot2)
library(lubridate)
library(scales)
library(ggpubr)
followed by chunk 2
{python ,echo=FALSE, message = FALSE, warning = FALSE,results = FALSE}
import os
path="/Users/-----"
os.chdir(path)
os.getcwd()
Chunk 1 works without any problem. However, as soon as I use import os it crashes. If I disable the following packages #library(ggplot2) #library(ggpubr) in chunk 1, then it does not crash. If I run chunk 2 before chunk 1 it crashes as soon as I load library(ggplot2).
I'm working with Miniconda (Python 3.6.13) as interpreter in Rstudio. Everything in the markdown works except for the combination of using ggplot2 (or ggpubr, but that's because it loads ggplot2) with any python chunk (i.e. I tried just using the following chunk #3
{python}
A=2
but this also crashed Rstudio. Any other people having similar issues? Is there any solution to this?
Solved the problem. It was not Rstudio that needed an update, but I was running an old version of R itself. I am still not sure why the problem persisted on my new work computer and not on my backup computer with the same versions of R, Rstudio and python (reticulate) running, but updating R fixes the problem.
Related
I am a big fan of Rstudio Cloud and would like to inter-grate R and Python by using the package Reticulate.
It looks like Rstudio Cloud is using python 2.7 (no problems with that). When I try to write Python Code in an R markdown document, nothing gets run.
---
title: "reticulate"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(reticulate)
py_config()
```
```{python}
import pandas
x = 4
```
Python code does not get run.
I am also finding that if I want to install python packages in an R script using reticulate. I have to create a virtual environment. What is the reason behind that?
library(reticulate)
virtualenv_create("r-reticulate")
virtualenv_install("r-reticulate", "scipy")
virtualenv_install("r-reticulate", "pandas")
If I use conda_install, I get an error message.
conda_create("r-reticulate")
Error: Unable to find conda binary. Is Anaconda installed?
conda_install("r-reticulate", "scipy")
Error: Unable to find conda binary. Is Anaconda installed?
The goal is to have python working in Rstudio cloud on R markdown. I can not install packages and execute code.
I just succeeded in getting Conda installed in Rstudio cloud after receiving the same error message as you1, so thought I'd share how I got this working.
I created two scripts:
to install miniconda (i think that's the step you're missing, and why Conda didn't work for you) and then restartSession for this to be accessible
to seperately store the commands for setting up Conda, with the running of this script passed as a command to the call to restartSession (because otherwise the commands are triggered before R has restarted, and they fail; sys.sleep() didn't seem to work, but this method did)
setup.R
setwd("/cloud/project") # to ensure students get required resources
install.packages("rstudioapi") # to restart R session w/ installations
install.packages("reticulate") # for python
reticulate::install_miniconda("miniconda") # for python
# Restart again to make sure all system things are loaded
# and then create a new Conda environment
rstudioapi::restartSession(command="source('nested_reticulate_setup.R')")
nested_reticulate_setup.R
reticulate::conda_create("r-reticulate")
reticulate::conda_install("r-reticulate", "scipy")
Sys.setenv(RETICULATE_PYTHON="/cloud/project/miniconda/envs/r-reticulate/bin/python")
reticulate::use_condaenv("r-reticulate")
osmnx <- reticulate::import("scipy")
Then if you make a call to scipy, eg scicpy$`__version__` , I believe it should work for you without that error you observed.
I couldn't find a solution to this issue elsewhere, so thought it worth responding to this old post in case it helps somebody some day. I am sure there are other ways of approaching this.
1 Perhaps for a different reason; i'll explain later in the post...
I'm running Python code in VS Code (1.28.2, with the Python, and Python Extension Pack extensions) and am using the 'Run Selection/Line in Python Terminal' (Shift+Enter) functionality to just run selected code in the Python terminal.
This has always worked well, but today I'm getting a new line added between each line of code in the terminal, i.e. if I ran:
import heapq
import pickle
the output in the terminal would be:
>>>import heapq
>>>
>>>import pickle
At first, this just seems like an annoyance, but any for loops or functions now come out with an indentation error...so essentially I can't successfully run any code.
I've tried re-installing VS Code as well as installing an older version of VS Code but all give the same problem.
It's so odd because it was working fine and then all of a sudden it changed. The only thing I can think of that has possibly changed is I installed the JSON Tools extension, but I don't believe this would change anything within Python (and I've tried uninstalling this, and not loading it again when reinstalling VS Code from scratch)
Any help would be greatly appreciated!
It's a bug that will be fixed in the 2018.9.1 release of the Python extension.
Maybe I'm missing something, but if the following code is the content of my Rmd file
```{r}
library(reticulate)
use_virtualenv("r-reticulate")
py_available(TRUE)
```
```{python}
a = 7
print(a)
```
```{r}
py$a
```
when I Knit the file, the output for the last chunk is 7 (as expected). On the other hand, clicking the run all button in Rstudio (or running chunks one by one), results on NULL for the last chunk.
Comparing with the R notebook example it seems like assigning something to flights in the python chunk should make py$flights available for R, but that doesn't seem the case.
Questions:
Is there a way to access from R a variable created in a Python chunk previously ran (not knit)? How to "export" to R a variable created within a python chunk?
What is a good reference to understand what happens when I click the run button in a python chunk of a Rmarkdown file?
EDIT: Ok so after seeing the first answers here, I did update both knitr and rmarkdown to the latest version, but still had the same problem.
I added py_available(TRUE) to my file to make sure it was initialized, still, last chunk results in 7 when knitted, but running chunks one-by-one results in
> py$a
Error in py_get_attr_impl(x, name, silent) :
AttributeError: 'module' object has no attribute 'a'
The problem is: Assigning a value to a in the python chunk isn't doing anything to py$a in the R environment. Maybe this "shared" environment between R and python isn't how the package is supposed to work? Also, for some extra information
> py_config()
python: /usr/bin/python
libpython: /usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
pythonhome: /usr:/usr
version: 2.7.14 (default, Sep 23 2017, 22:06:14) [GCC 7.2.0]
numpy: /usr/lib/python2.7/dist-packages/numpy
numpy_version: 1.12.1
python versions found:
/usr/bin/python
/usr/bin/python3
Rmarkdown / knitr:
Running the chunks:
Running the chunks without knitting the document is not supported so far. See here: https://github.com/yihui/knitr/issues/1440 or Reticulate not sharing state between R/Python cells or Python/Python cells in RMarkdown.
Edit: Workaround by Freguglia:
"Workaround is to turn python chunks into R chunks and just wrap the whole content in the py_run_string() function, so whatever you assign in that piece of code is accessible from R by py$variable_name."
Knitting the document:
One way is to upgrade knitr as suggested above, but you dont have to and you also dont need RStudio daily build.
If you have a version of knitr prior to 1.18, you can include:
```{r setup, include = FALSE}
knitr::knit_engines$set(python = reticulate::eng_python)
```
, see here: https://rstudio.github.io/reticulate/articles/r_markdown.html#engine-setup.
Python:
If it doesnt work ensure the python connection is running outside of rmarmdown/knitr:
py_run_string("x = 10"); py$x.
In case that also doesnt work, you should check:
py_available() and py_numpy_available().
If it returns FALSE: Try to initialize it with: py_available(TRUE).
If that´s still a no - check your config:
py_config()
It will give you further hints on the problem:
Examples for me were: different bit versions of R and python (32 vs 64) or somehow i ran into trouble having installed both Python2.7 and seperately Anaconda.
This is fixed in current RStudio Desktop e.g. 1.2.1114. But if you are like me stuck with RStudio Server Pro 1.1.456 a better workaround than using py_run_string might be to use reticulate::repl_python() which gives you a Python console within the R console and allows you to run your python chunks by copy pasting them into the console.
workaround:
working:
You must use the Rstudio daily build (source) and upgrade knitr, rmarkdown to the latest version.
> packageVersion("rmarkdown")
[1] ‘1.9’
> packageVersion("knitr")
[1] ‘1.20’
I'm running some basic code in the Visual Studio Code editor on MacOSX:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show()
...and can't seem to get the png/svg file image to come up after running this. This also doesn't stop executing and I have to manually terminate the process. However, if I run this directly in the Terminal (each line of code line for line) I get the resulting image. One work-around is to just save the file (plt.savefig('foo.png')). This seems to work - the image is saved in the specified file location. However, it would be good to just see the image come up after running the code.
When running matplotlib codes from the terminal, I experience the same kind of hanging of the application after saving the image to a file. In this case, one 'workaround' that has always worked for me is to turn off blocking. Basically alter your code in this way:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 20, 100)
plt.plot(x, np.sin(x))
plt.show(block=False)
input('press <ENTER> to continue')
It's not perfect, but the image is saved correctly and the application stops after you hit ENTER in the terminal. Hope this helps.
I am having a similar issue and I think it is a problem with the exact version of python that vs code is using to run the code.
For reference, I have vscode version 1.52.1 on a mac os Catalina. I installed python via anaconda and created a new environment for python 2.7 (tried with python 3.8 too). I open VSCode by calling code . from the folder I have my simple python codes saved in.
Like OP, I could reproduce the figure if I were to run the code from a python instance called from terminal but not from vscode.
MY SOLUTION:
From vscode, select as python interpreter the one found in /usr/bin/python not the one in ~/opt/anaconda3/env/python27/bin/python
But this is weird, because from a separate terminal window which python returns ~/opt/anaconda3/env/python27/bin/python. This suggests me (though I am no python expert) that there is an issue within vscode about linking the right interpreter with the libraries. Frankly being new to vscode, I find the need to pay attention to these details quite concerning.
I got the same problem, which was driving me nuts. The image was displayed when using Jupyter Notebooks, but not always when using VS Code. I just added one last line_ plt.show() - IMHO unnecessarily - but this worked well.
import matplotlib.pyplot as plt
import matplotlib.image as img
im = img.imread('myimage.png')
plt.imshow(im)
plt.show() # <- added this line and now the image shows every time!
I faced the same issue and here's what I did to solve it.
Since the code runs without any error but also does not generate any plot and needs to be terminated manually, it's difficult to figure out what's going on. I tried running python2.7 test.py
This works, plot is generated but python3 test.py does not work.
So, here's what you need to do -
Run, pip install matplotlib --upgrade to upgrade the matplotlib. This does not resolve the issue but now the error is printed.
"RuntimeError: Python is not installed as a framework" ......
So, finally, to solve the problem, refer to Working with Matplotlib on macOS
Since, I am using Anaconda, all I need to do is conda install python.app and then use pythonw to run all scripts. I hope you also find the solution to your particular case from the FAQ.
Overall, it's a Matplotlib problem, so upgrading (or reinstalling) and trying with different Python versions should get you going.
I've been utilizing jupyter notebooks for awhile and typically place an
import pdb;pdb.set_trace()
in my notebook, which works as expected. Recently, I removed all Python related packages from my Windows machine and installed Enthought Canopy 32-bit. Now when trying to step through code via pdb I get the following
> <ipython-input-6-fdf950c00f74>(1)<module>()->None
-> import pdb;pdb.set_trace()
(Pdb) n
> c:\users\<user>\appdata\local\enthought\canopy32\user\lib\site- packages\ipython\core\interactiveshell.py(2888)run_code()
-> sys.excepthook = old_excepthook
It seems to jump into interactiveshell.py and never gets back to the actual code I'm wanting to step through. I've noticed this also happens in the 64 bit distro of Enthought