[英]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.
看起来 Rstudio Cloud 正在使用 python 2.7(没有问题)。 当我尝试在 R markdown 文档中编写 Python 代码时,没有任何运行。
---
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 代码无法运行。
我还发现,如果我想使用 reticulate 在 R 脚本中安装 python 包。 我必须创建一个虚拟环境。 这背后的原因是什么?
library(reticulate)
virtualenv_create("r-reticulate")
virtualenv_install("r-reticulate", "scipy")
virtualenv_install("r-reticulate", "pandas")
如果我使用 conda_install,我会收到一条错误消息。
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?
目标是让 python 在 R markdown 上的 Rstudio 云中工作。 我无法安装包和执行代码。
在收到与您1相同的错误消息后,我刚刚成功地将 Conda 安装在 Rstudio 云中,所以我想分享一下我是如何工作的。
我创建了两个脚本:
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")
然后,如果您调用 scipy,例如scicpy$`__version__`
,我相信它应该对您有用,而不会出现您观察到的错误。
我在其他地方找不到这个问题的解决方案,所以认为值得回复这篇旧帖子,以防有一天它对某人有所帮助。 我相信还有其他方法可以解决这个问题。
1也许是出于不同的原因; 我会在后面的帖子中解释...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.