一个 Jupyter 笔记本中的 R 和 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39008069/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:45:29  来源:igfitidea点击:

R and Python in one Jupyter notebook

pythonrpython-2.7ipythonjupyter-notebook

提问by Sailendra Pinupolu

Is it possible to run R and Python code in the same Jupyter notebook. What are all the alternatives available?

是否可以在同一个 Jupyter notebook 中运行 R 和 Python 代码。有哪些可用的替代方案?

  1. Install r-essentials and create R notebooks in Jupyter.
  2. Install rpy2 and use rmagic functions.
  3. Use a beaker notebook.
  1. 在 Jupyter 中安装 r-essentials 并创建 R 笔记本。
  2. 安装 rpy2 并使用 rmagic 函数。
  3. 使用烧杯笔记本。

Which of above 3 options is reliable to run Python and R code snippets (sharing variables and visualizations) or is there a better option already?

以上 3 个选项中哪一个对于运行 Python 和 R 代码片段(共享变量和可视化)是可靠的,或者是否已经有更好的选择?

回答by uut

Yes, it is possible! Use rpy2.

对的,这是可能的!使用 rpy2。

You can install rpy2 with: pip install rpy2

您可以使用以下命令安装 rpy2: pip install rpy2

Then run %load_ext rpy2.ipythonin one of your cells. (You only have to run this once.)

然后%load_ext rpy2.ipython在你的一个单元格中运行。(你只需要运行一次。)

Now you can do the following:

现在您可以执行以下操作:

Python cell:

蟒蛇细胞:

# enables the %%R magic, not necessary if you've already done this
%load_ext rpy2.ipython

import pandas as pd
df = pd.DataFrame({
    'cups_of_coffee': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
    'productivity': [2, 5, 6, 8, 9, 8, 0, 1, 0, -1]
})

R cell:

R 细胞:

%%R -i df -w 5 -h 5 --units in -r 200
# import df from global environment
# make default figure size 5 by 5 inches with 200 dpi resolution

install.packages("ggplot2", repos='http://cran.us.r-project.org', quiet=TRUE)
library(ggplot2)
ggplot(df, aes(x=cups_of_coffee, y=productivity)) + geom_line()

And you'll get your pretty figure plotting data from a python Pandas DataFrame.

您将从 python Pandas DataFrame 中获得漂亮的图形绘制数据。

回答by Abhimanu Kumar

Using @uut's answer for running R in a jupyter notebook within python kernel (in MacOS), the following worked for me.

使用@uut 的答案在 python 内核(在 MacOS 中)的 jupyter notebook 中运行 R,以下内容对我有用。

%%Rshould always be at the start of the cell else you will get the error as shown in figure belowsyntax error if %%R not at the top of the cell

%%R应该总是在单元格的开头,否则你会得到如下图所示的错误如果 %%R 不在单元格顶部,则语法错误

The following is the right way:Right way to invoke R within python kernel

以下是正确的方法:在 python 内核中调用 R 的正确方法

Also %load_ext rpy2.ipythonshould come before %%Rhence put it in a different cell above it as shown in the figures.

%load_ext rpy2.ipython应该%%R放在之前,因此将它放在它上面的不同单元格中,如图所示。

回答by Allen Wang

UPDATE April 2018,

2018 年 4 月更新,

RStudio has also put out a package: https://blog.rstudio.com/2018/03/26/reticulate-r-interface-to-python/

RStudio 也推出了一个包:https: //blog.rstudio.com/2018/03/26/reticulate-r-interface-to-python/

for which it is possible to run multiple code chunks in different languages using the R markdown notebook, which is similar to a jupyter notebook.

为此,可以使用类似于 jupyter 笔记本的 R markdown notebook 以不同的语言运行多个代码块。

In my previous post, I said that the underlying representation of objects is different. Actually here is a more nuanced discussion of the underlying matrix representation of R and python from the same package: https://rstudio.github.io/reticulate/articles/arrays.html

在我之前的帖子中,我说过对象的底层表示是不同的。实际上,这里是对来自同一包的 R 和 python 的底层矩阵表示的更细致的讨论:https: //rstudio.github.io/reticulate/articles/arrays.html

Old post:

旧帖子:

It will be hard for you to use both R and Python syntax in the same notebook, mostly because the underlying representation of objects in the two languages are different. That said, there is a project that does try to allow conversion of objects and different languages in the same notebook: http://beakernotebook.com/features

您很难在同一个 notebook 中同时使用 R 和 Python 语法,主要是因为这两种语言中对象的底层表示不同。也就是说,有一个项目确实尝试允许在同一笔记本中转换对象和不同语言:http: //beakernotebook.com/features

I haven't used it myself but it looks promising

我自己没用过,但看起来很有希望

回答by rm1104

SoS kernel is another option.

SoS 内核是另一种选择。

Don't know how well it performs yet, just started using it.

不知道它的性能如何,刚开始使用它。

The SoS kernel allows you to run different languages within the same notebook, including Python and R.

SoS 内核允许您在同一笔记本中运行不同的语言,包括 Python 和 R。

SoS Polyglot Notebook - Instructions for Installing Desired Languages

SoS 多语言笔记本 - 安装所需语言的说明

Here is an example of a notebook with Python and R cells.

这是带有Python 和 R 单元格的笔记本示例。



*Update:

*更新:

In terms of sharing variables, one can use the magics %useand %with. "SoS automatically shares variables with names starting with sosamong all subkernels"1.

在共享变量方面,可以使用魔法%use%with. “SoS 自动在所有子内核中共享名称以sos开头的变量” 1

Ex.

前任。

Starting cell in R:

R中的起始单元格:

%use R
sos_var=read.csv('G:\Somefile.csv')
dim(sos_var)

Output:

输出:

51  13

Switching to python:

切换到python:

%with Python3
sos_var.shape

Output:

输出:

(51, 13)

回答by Dmitry Petrov

I would not recommend to use two languages in a single Notebook. Instead, you can orchestrate R and Python code in project level by connecting them on input\output file base. Data science tools like DVCcan help you in order to do that.

我不建议在一个 Notebook 中使用两种语言。相反,您可以通过将 R 和 Python 代码连接到输入\输出文件库来编排项目级别的 R 和 Python 代码。像DVC这样的数据科学工具可以帮助你做到这一点。

You might find some code examples in this blog post: Best practices of orchestrating Python and R code in ML projects

您可能会在这篇博文中找到一些代码示例:在 ML 项目中编排 Python 和 R 代码的最佳实践