Sourcing a python virtualenv inside R markdown?

125 views
Skip to first unread message

tere...@ucdavis.edu

unread,
Jul 28, 2017, 4:20:21 PM7/28/17
to Davis R Users' Group
Hello All,

Does anyone have experience sourcing a python virtualenv inside of R markdown? I tried what I thought was the intuitive thing:

```{r}
getwd()
```

```{bash}
source eteEnv/bin/activate
```

```{python}
from ete3 import NCBITaxa
ncbi = NCBITaxa()
ncbi.update_taxonomy_database()
```

But this was not successful. I also tried calling the virtualenv with the package reticulate using `use_virtualenv(virtualenv, required = TRUE)`, and this was not successful either.

Does anyone have experience with this?

Best,
Taylor

Ryan Peek

unread,
Jul 28, 2017, 4:28:30 PM7/28/17
to davi...@googlegroups.com
A question along these exact lines came up today, see https://twitter.com/noamross/status/890923806608457729 for discussion. Short answer is system/reticulate seems only option.

Alternatively, Nick Santos recently shared a great tool (Windows only at the moment) going from the other direction, using R calls from python. Check it out:



Thanks
R

--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/davis-rug.
For more options, visit https://groups.google.com/d/optout.



--

 
"When we try to pick out anything by itself, we find it hitched to everything else in the universe."
John Muir (My First Summer in the Sierra, 1911)
-----------------------------------------------------
Ryan Peek
PhD Candidate, Graduate Group in Ecology
Center for Watershed Sciences, UC Davis

-----------------------------------------------------

C. Titus Brown

unread,
Jul 28, 2017, 4:34:37 PM7/28/17
to davi...@googlegroups.com
Hi Taylor,

the bash and python namespaces are almost certainly quite separate -
RMarkdown is presumably choosing a specific Python interpreter to
run.

Some googling

'python2 python3 rmarkdown'

and

'which python interpreter rmarkdown'

suggests that the answer is to specify the interpreter you want to use
via engine.path - see

http://rmarkdown.rstudio.com/authoring_knitr_engines.html

Here you would specify engine.path to be 'eteEnv/bin/python'.

Let me know if that works!

cheers,
--titus
> --
> Check out our R resources at http://d-rug.github.io/
> ---
> You received this message because you are subscribed to the Google Groups "Davis R Users' Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to davis-rug+...@googlegroups.com.
> Visit this group at https://groups.google.com/group/davis-rug.
> For more options, visit https://groups.google.com/d/optout.


--
C. Titus Brown, ctb...@ucdavis.edu

Alex Mandel

unread,
Jul 28, 2017, 5:53:24 PM7/28/17
to davi...@googlegroups.com, tere...@ucdavis.edu
What about using a Jupyter notebook with a mix of R and Python in
different chunks?

Alex Mandel, PhD

Center for Spatial Sciences
http://spatial.ucdavis.edu
Geospatial and Farming Systems Research Consortium
http://gfc.ucdavis.edu
University of California, Davis

Taylor Reiter

unread,
Jul 28, 2017, 6:35:09 PM7/28/17
to davi...@googlegroups.com
Thank you all for the suggestions!

Alex, I have used jupyter before and mixed kernels and like that solution quite a bit, however here I specifically wanted an .Rmd file that would render to an html...and wanted to know if I could use Rmarkdown for this sort of thing in the future :)

Titus, before you responded, tried: 

```
source eteEnv/bin/activate
Rscript -e "library(knitr); knit('oil_species_taxonomy.Rmd')" 
```
from the command line andthe document found the right python and rendered, however as an .md document, and not as a .html.

I tried your fix and it worked like a charm...thank you!!!

My python code blocks now read:

```{python, , engine.path = "./eteEnv/bin/python"}
from ete3 import NCBITaxa
...
```

Best,
Taylor


--
Check out our R resources at http://d-rug.github.io/
---
You received this message because you are subscribed to a topic in the Google Groups "Davis R Users' Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/davis-rug/BxxGYEeNLK4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to davis-rug+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages