【问题标题】:rpy2 installation on macbook on Jupyter Notebook在 Jupyter Notebook 上的 macbook 上安装 rpy2
【发布时间】:2021-11-24 18:10:20
【问题描述】:

试图在终端上安装rpy2 模块,但没有任何动作。

尝试在单元格上直接安装rpy2

!pip install rpy2
%load_ext rpy2.ipython
%%R -i data 
data <- list('0.47', '-0.36', '-0.5', '0.2', '0.35', '1.82', 
             '-0.78', '-0.91', '0.36', '-1.74', '0.24', '0.76', 
             '0.57', '2.32', '1.55', '-1.31', '-0.09', '-0.02', 
             '-0.07', '-0.19', '-0.25', 
             '-1.09', '0.64', '1.22', '-0.56', '1.76', '0.13', 
             '1.33', '-0.74', '-1.15', '1.63', '1.04', '-0.26', 
             '0.02', '-1.2', '0.37', '0.43', '0.04', '1.34', 
             '0.57', '0.76', '-1.25', '-0.05', '0.12', '0.8', 
             '-0.99', '-0.11', '-0.54', '-0.08', '-0.04', '-0.76', 
             '-0.8', '0.35', '1.54', '-0.99', '-0.35', '-0.28', '0.45', 
             '-0.04', '-0.06', '0.02', '0.58', '-0.32', '-0.1', '0.28', 
             '0.3', '-0.36', '0.81', '0.79', '0.21', '1.81', '0.19', '0.84', 
             '0.2', '-0.06', '-0.11', '-1.4', '-2.08', '0.88', '-0.14',
             '-0.96', '1.3', '0.06', '-0.37', '1.49', '-0.91', 
             '1.14', '-1.05', '1.49', '-0.79', '2.02', '0.38', '2.4', '1.25', 
             '0.5', '1.11', '-0.54', '-0.1', '0.63', '1.01')
num <- as.numeric(unlist(data))
shapiro.test(num)
shapiro.test

结果显示

 File "<ipython-input-3-0be657748cce>", line 20
    num <- as.numeric(unlist(data))
           ^
SyntaxError: invalid syntax

还是不行

我只想将 R 代码附加到我的 Jupyter 笔记本上。

data <- list('0.47', '-0.36', '-0.5', '0.2', '0.35', '1.82', 
             '-0.78', '-0.91', '0.36', '-1.74', '0.24', '0.76', 
             '0.57', '2.32', '1.55', '-1.31', '-0.09', '-0.02', 
             '-0.07', '-0.19', '-0.25', 
             '-1.09', '0.64', '1.22', '-0.56', '1.76', '0.13', 
             '1.33', '-0.74', '-1.15', '1.63', '1.04', '-0.26', 
             '0.02', '-1.2', '0.37', '0.43', '0.04', '1.34', 
             '0.57', '0.76', '-1.25', '-0.05', '0.12', '0.8', 
             '-0.99', '-0.11', '-0.54', '-0.08', '-0.04', '-0.76', 
             '-0.8', '0.35', '1.54', '-0.99', '-0.35', '-0.28', '0.45', 
             '-0.04', '-0.06', '0.02', '0.58', '-0.32', '-0.1', '0.28', 
             '0.3', '-0.36', '0.81', '0.79', '0.21', '1.81', '0.19', '0.84', 
             '0.2', '-0.06', '-0.11', '-1.4', '-2.08', '0.88', '-0.14',
             '-0.96', '1.3', '0.06', '-0.37', '1.49', '-0.91', 
             '1.14', '-1.05', '1.49', '-0.79', '2.02', '0.38', '2.4', '1.25', 
             '0.5', '1.11', '-0.54', '-0.1', '0.63', '1.01')
num <- as.numeric(unlist(data))
shapiro.test(num)
shapiro.test

【问题讨论】:

  • 在您的笔记本中,创建新单元并运行!pip install rpy2,它将安装包,您将不必使用 conda。我在 conda 遇到过这种问题。
  • 此终端窗口仅用于输出内容,不用于输入。如果您没有看到提示(以 >、$、[] 等开头的行),您可以预期这仅用于查看消息。
  • 你真的想用 R 和 python 环境建立一个多语言笔记本系统来读取数字向量吗?使用readr::write_lines 导出R 中的数据并使用[float(x) for x in open("foo.txt", "r").readlines()] 在python 中读取文件。它要简单得多。 Jupyter 并非旨在轻松使用多种语言,而语言旨在仅读取文本文件。
  • 请告诉我你要使用的python函数。混合语言并非易事。顺便说一句:rpy2 也要求您安装 R。这必须在 juptyter 内核中可以访问。
  • 你需要写%%R -o data data &lt;- list(1,2,3)而不是-i,因为数据是一个R输出。您也可以使用 IRkernel 在 jupyter 中仅使用 R。为什么要强制使用python?

标签: python r jupyter-notebook


【解决方案1】:

您的示例仅显示了没有使用任何 python 函数的 R 代码。

选项 1(简单,无需 jupyter 和 python)

如果你真的不需要 Jupyter 和 python,你可以在 RStudio 中使用 RMarkdon 创建包含 R 代码单元和文本的文档。

选项 2(硬,使用 jupyter 和 python)

  1. 安装 R 和 python
  2. 安装 Jupyter
  3. 在 jupyter 单元中使用 ! pip install rpy2 安装 rpy2
  4. 在笔记本中运行 R 代码:
import rpy2
%load_ext rpy2.ipython
%%R -o data 
data <- list(1,2,3)

选项 3(中,jupyter 仅使用 R 不使用 python)

  1. 安装 jupyter
  2. 安装IRkernel
  3. 启动 juypter 并选择 R 内核
  4. 只在您的 jupyter 单元中编写 R 代码:

【讨论】:

  • data &lt;- list(1,2,3) 是什么意思?如何将上面的data 转换为您建议的形式
  • 这是任何 R 代码的占位符。只需将其替换为 data &lt;- list('0.47', '... 即可
  • 这是什么意思?数据已经采用data &lt;- list('0.47', '... 的形式
  • 我添加了截图
  • 你怎么能运行它?使用内核 R?我明白了
猜你喜欢
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多