简体   繁体   中英

Python Reticulate not working in Rstudio Cloud

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 you 1 , so thought I'd share how I got this working.

I created two scripts:

  1. 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
  2. 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM