How to run python script in Rstudio

╄→гoц情女王★ 提交于 2019-12-08 07:59:52

问题


I have a problem on how to run a python script from Rstudio?

My initial idea is to grab the python script from a GitHub repository then run it in R, I grabbed python code by using script <- getURL(URL, ssl.verifypeer = FALSE), from RCurl package, I was stuck on how to run Python code without storing the script as a file in the working directory, that is, running the R variable script above directory in Rstudio.

I did know python.load() in _rPython_ package in R could help to run Python script, but it requires the .py file as the first argument. I would like to find a way without storing the Python script as a file.

Thank you in advance if you have any idea of this problem.


回答1:


Make sure you're running an R Markdown file and have reticulate installed.

Load and configure your Python version:

```{r setup, include = FALSE}
library(reticulate)
use_python("usr/local/bin/python")
```

Then, any python code can be called as follows:

```{python}
# write python
# code here
```

If you create any global python objects and want to use them with R code, simply preface them with py$; e.g. to access a dataframe created with python called my_data in an R chunk:

```{r}
head(py$my_data)
```

More details can be found here.



来源:https://stackoverflow.com/questions/49618485/how-to-run-python-script-in-rstudio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!