Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run an ipython notebook from R studio

Tags:

r

rstudio

I've got an ipython notebook I would like to run before running analysis in R.

I've set up a code chunk in my R notebook to run ipython as follows

```{python, engine = 'path/to/ipython'}

import os #analogous to library().  Executres

os.chdir('path/to/analysis') #analogous to setwd(). Execures

%runDataExtraction.ipynb  #problem here
```

Here is the error

thon: Classes/Stat Comp  File "/var/folders/_1/hdrhn2y9719c6vnr54tsk2tc0000gn/T/RtmpxWbI79/chunk-coded987fa22c02.", line 6
    %run DataExtraction.ipynb
    ^
SyntaxError: invalid syntax

R tells me that there is a syntax error in that last line of code. The code works fine if run in ipython. Even a bash chunk tells me something is wrong.

Chunk is

```{bash}
cd path/to/analysis

jupyter nbconvert --execute DataExtraction.ipynb
```

Error is

/var/folders/_1/hdrhn2y9719c6vnr54tsk2tc0000gn/T/RtmpxWbI79/chunk-coded986d89ee10.: line 3: jupyter: command not found

This error is particularly weird because the jupyter command definitely works in the command line.

Has anyone else run into similar problems?

EDIT:

Little out of the way, but I run a bash command from ipython, and everything works.

```{python, engine.path = 'path/to/ipython'}

import os #analogous to library().  Executres

os.chdir('path/to/analysis') #analogous to setwd(). 

bashCommand = "jupyter nbconvert --execute DataExtraction.ipynb"
import subprocess
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()


```
like image 500
Demetri Pananos Avatar asked Oct 27 '25 14:10

Demetri Pananos


1 Answers

Here is a solution I came across

```{python, engine.path = 'path/to/ipython'}

import os #analogous to library().  Executres

os.chdir('path/to/analysis') #analogous to setwd(). 

bashCommand = "jupyter nbconvert --execute DataExtraction.ipynb"
import subprocess
process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
output, error = process.communicate()


```
like image 81
Demetri Pananos Avatar answered Oct 29 '25 06:10

Demetri Pananos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!