簡體   English   中英

Python 網狀在 Rstudio 雲中不起作用

[英]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 雲中,所以我想分享一下我是如何工作的。

我創建了兩個腳本:

  1. 安裝 miniconda (我認為這是您缺少的步驟,以及為什么 Conda 不適合您),然后 restartSession 以便可以訪問
  2. 單獨存儲用於設置 Conda 的命令,將此腳本的運行作為命令傳遞給restartSession 調用(因為否則命令在 R 重新啟動之前觸發,並且它們失敗;sys.sleep() 似乎沒有工作,但這種方法確實)

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM