Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify rmarkdown to use Python3 instead of Python 2?

I am trying to run python as rmarkdwon code chunks. I was successful but rmarkdown by default uses Python2 and I want it to use Python 3. I am running it on Ubuntu with Python 2.7.6 installed and I installed anaconda with Python 3.5, which is the one I want rmarkdown use.

Here is the code and output of the python chunk in rmarkdown

import sys
print (sys.version)

and the output:

2.7.6 (default, Jun 22 2015, 17:58:13) 

Any ideas?

like image 900
COLO Avatar asked Aug 21 '16 21:08

COLO


People also ask

Is there an R Markdown equivalent in Python?

Overview. The reticulate package includes a Python engine for R Markdown that enables easy interoperability between Python and R chunks. Python chunks behave very similar to R chunks (including graphical output from matplotlib) and the two languages have full access each other's objects.

How do I use Python code in R Markdown?

To add a Python code chunk to an R Markdown document, you can use the chunk header ```{python} , e.g., ```{python} print("Hello Python!") ```


1 Answers

You can select your desired python version, as default, with an R chunk:

```{r setup, echo=FALSE}
library(knitr)
opts_chunk$set(engine.path = '/usr/bin/python3')
```

From now on your python chunks will use Python3:

```{python}
import sys
print(sys.version)
```

This way to select python version avoids to add the engine.path variable to every code chunk.

like image 131
Enrique Pérez Herrero Avatar answered Oct 19 '22 09:10

Enrique Pérez Herrero