[英]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.