在 Rstudio 中运行 python/bash 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34263441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running python/bash code in Rstudio
提问by cantdutchthis
I am using Rstudio for my day to day R stuff. Sometimes, I'd like to use some python/bash for the parts that R isn't super good at. Curiously enough I've noticed if I start an new RMarkdown document, that the following code works:
我将 Rstudio 用于我的日常 R 工作。有时,我想对 R 不太擅长的部分使用一些 python/bash。奇怪的是,我注意到如果我开始一个新的 RMarkdown 文档,以下代码有效:
```{r engine='python'}
print "Hello" + "World"
import random
print random.random()
```
Rstudio can run me some python. This is very useful, but preferably I would be able to run this not just via the markdown feature but through a console as well. In the release notesit is suggested that there is support for syntax highlighting.
Rstudio 可以运行我一些 python。这非常有用,但最好我不仅可以通过降价功能还可以通过控制台运行它。在发行说明中,建议支持语法高亮。
I wonder, is there any method to connect a new console to Rstudio such that we could also do some python/bash from the IDE? It certainly seems like Rstudio has a notion of how to connect to python. The end goal would be to create .Rmd
documents and be able to edit/interact with them that have the following structure:
我想知道,是否有任何方法可以将新控制台连接到 Rstudio,以便我们也可以从 IDE 执行一些 python/bash?看起来 Rstudio 对如何连接到 python 有一个概念。最终目标是创建.Rmd
文档并能够编辑/交互具有以下结构的文档:
# Use Case
Connect to an api that is supported in python
```{r engine='python', highlight=TRUE}
data = foobar_api.get(1000)
file_loc = open("~/data/filename.csv", "w")
file_loc(data)
file_loc.close()
```
Then analyse with R again.
```{r}
df <- read.csv("~/data/filename.csv")
summary(df)
```
采纳答案by cantdutchthis
First you need to set the knitr
options.
首先,您需要设置knitr
选项。
```{r}
knitr::opts_chunk$set(engine.path = list(python = '/anaconda/bin/python'))
```
From that point on it just works.
从那时起它就起作用了。
```{python}
import this
```
回答by Tobias Verbeke
If you use Architector plain Eclipsewith StatET, you can install the PyDevplug-ins and launch and interact with Python consoles as easily as you would do with your R Consoles (and, there is, of course, ample support for editing and processing .Rmd files)
如果您使用Architect或带有StatET 的普通Eclipse,您可以安装PyDev插件并像使用 R 控制台一样轻松启动并与 Python 控制台交互(当然,还有对编辑和处理的充分支持.Rmd 文件)
回答by FvD
This is an example of knitr at it's best, where it allows for multiple language engines. You might consider editing the file for just these cases in VIM, because you can do something fun that is close to what you are asking: select the text and then type:
这是 knitr 最好的一个例子,它允许多语言引擎。您可能会考虑在 VIM 中仅针对这些情况编辑文件,因为您可以做一些与您要求的很接近的有趣事情:选择文本,然后键入:
:'<,'>!python
to execute in python and
在 python 中执行和
:<','>!R --no-save
To execute in R. See the answers to this questionfor more details.
在 R 中执行。有关更多详细信息,请参阅此问题的答案。
The above does not solve the use case completely, because the selected text is replaced by the output of the command (starting with the R version etc. in the case of a simple R command). It is possible however, to send the output to a different buffer (read: window) using this vimtip.
以上并没有完全解决用例,因为选择的文本被命令的输出替换(在简单的R命令的情况下从R版本等开始)。但是,可以使用此 vimtip将输出发送到不同的缓冲区(读取:窗口)。
The VIM-mode in RStudio is nothing short of excelent (it even supports visual block mode). But it cannot emulate everything and :!python
in RStudio will not work. I often have the document I'm working on open in both RStudio and VIM, and the above might be a reason for you to do the same for Rmd documents with mixed languages.
RStudio 中的 VIM 模式非常出色(它甚至支持可视块模式)。但它无法模拟所有内容,并且:!python
在 RStudio 中将无法正常工作。我经常在 RStudio 和 VIM 中打开我正在处理的文档,以上可能是您对混合语言的 Rmd 文档执行相同操作的原因。