python中r的用法_reticulate:在R中使用Python

reticulate 是 R 的一个程序包,允许在 R 中使用 Python。该包本质上是提供了本地 Python 环境的 R 接口,在 R 中调用该接口完成 Python 代码的执行。

该包提供以下几种方式在 R 中执行 Python:R 中调用 Python 模块完成一些操作

在 Rmarkdown 的 Cell 中直接运行 Python 代码Python Cell 和 R Cell 创建的对象可以相互转化、访问

Python Cell 的结果可以打印出来,包括 matplotlib 图形输出

在 R 中执行 Python 脚本:*.py

在 R 脚本中执行 Python 代码片段

library(reticulate)

1、配置 Python 环境

有四种配置方式,具体查看

# 1.直接指定Python执行程序

use_python()

# 2.指定Python conda环境

use_condaenv()

# 3.指定Python虚拟环境

use_virtualenv()

# 4.指定Python miniconda环境

use_miniconda()

# 查看配置的环境

py_config()

miniconda 环境

如果不太懂如何配置 Python 环境,推荐一行代码安装 miniconda 环境,之后便可在 R 中执行 Python

# 安装miniconda环境

install_miniconda()

# 显示miniconda安装地址

miniconda_path()

# 更新miniconda环境

miniconda_update()

2、在 R 中调用 Python 模块

# 调用os模块的listdir()函数

os <- import("os")

os$listdir("./")

> [1] ".Rproj.user" "convert.R" "reticulate.Rmd" "Reticulate.Rproj"

> [5] "Rscript.R" "summary.html" "summary.md" "summary.nb.html"

> [9] "summary.Rmd" "test_pyscript.py"

# 调用seaborn模块的load_dataset()函数

# 需要seaborn模块已安装

sns <- import("seaborn")

tips <- sns$load_dataset("tips")

print(head(tips))

> total_bill tip sex smoker day time size

> 1 16.99 1.01 Female No Sun Dinner 2

> 2 10.34 1.66 Male No Sun Dinner 3

> 3 21.01 3.50 Male No Sun Dinner 3

> 4 23.68 3.31 Male No Sun Dinner 2

> 5 24.59 3.61 Female No Sun Dinner 4

> 6 25.29 4.71 Male No Sun Dinner 4

在 R 中安装 python 模块

# 安装seaborn绘图库

# pip = T指定从pip安装,默认从conda安装

py_install("seaborn", pip = T)

# 查看seaborn模块是否已安装

py_module_available("seaborn")

> [1] TRUE

3、在 RMarkdown 中直接使用 Python

注意 Cell 要设置为 {python}

import seaborn as sns

tips = sns.load_dataset("tips")

tips.head()

sns.scatterplot(x="total_bill", y="tip", data=tips, hue="smoker")

> total_bill tip sex smoker day time size

> 0 16.99 1.01 Female No Sun Dinner 2

> 1 10.34 1.66 Male No Sun Dinner 3

> 2 21.01 3.50 Male No Sun Dinner 3

> 3 23.68 3.31 Male No Sun Dinner 2

> 4 24.59 3.61 Female No Sun Dinner 4

Rmarkdown 中 Python 与 R 对象相互调用

设置一些 R 对象(R Cell)

x <- c(1, 2, 3)

y <- data.frame(a = c(1, 2), b = c(3, 4))

Python 中调用上述 R 对象(Python Cell)

r.x

> [1.0, 2.0, 3.0]

r.y

> a b

> 0 1.0 3.0

> 1 2.0 4.0

设置一些 Python 对象(Python Cell)

import pandas as pd

m = [1, 2, 3]

n = pd.DataFrame([[1, 2], [3, 4]], columns=["a", "b"])

R 中调用上述 Python 对象(R Cell)

py$m

> [1] 1 2 3

py$n

> a b

> 1 1 2

> 2 3 4

Python 与 R 对象相互转换的方式

R

equation?tex=%5Cto Python

设置一些 R 对象(R Cell)

A <- 1

B <- c(1, 2, 3)

C <- c(a = 1, b = 2, c = 3)

D <- matrix(1:4, nrow = 2)

E <- data.frame(a = c(1, 2), b = c(3, 4))

G <- list(1, 2, 3)

H <- list(c(1, 2), c(3, 4))

I <- list(a = c(1, 2), b = c(3, 4))

J <- function(a, b) {

return(a + b)

}

K1 <- NULL

K2 <- T

K3 <- F

上述 R 对象转为 Python 对象(Python Cell)

r.A

> 1.0

type(r.A)

>

r.B

> [1.0, 2.0, 3.0]

type(r.B)

>

r.C

> [1.0, 2.0, 3.0]

type(r.C)

>

r.D

> array([[1, 3],

> [2, 4]])

type(r.D)

>

r.E

> a b

> 0 1.0 3.0

> 1 2.0 4.0

type(r.E)

>

r.G

> [1.0, 2.0, 3.0]

type(r.G)

>

r.H

> [[1.0, 2.0], [3.0, 4.0]]

type(r.H)

>

r.I

> {'a': [1.0, 2.0], 'b': [3.0, 4.0]}

type(r.I)

>

r.J

> .python_function at 0x000001AE204ECE18>

type(r.J)

>

r.J(2, 3)

> 5

r.K1

type(r.K1)

>

r.K2

> True

type(r.K2)

>

r.K3

> False

type(r.K3)

>

Python

equation?tex=%5Cto R

设置一些 Python 对象(Python Cell)

A = 1

B = [1, 2, 3]

C = [[1, 2], [3, 4]]

D1 = [[1], 2, 3]

D2 = [[1, 2], 2, 3]

E = (1, 2, 3)

FF = ((1, 2), (3, 4))

G = ((1, 2), 3, 4)

H = {"a": [1, 2, 3],

"b": [2, 3, 4]

}

I = {"a": 1,

"b": [2, 3, 4]

}

def J(a, b):

return a + b

上述 Python 对象转为 R 对象(R Cell)

py$A

> [1] 1

class(py$A)

> [1] "integer"

py$B

> [1] 1 2 3

class(py$B)

> [1] "integer"

py$C

> [[1]]

> [1] 1 2

>

> [[2]]

> [1] 3 4

class(py$C)

> [1] "list"

py$D1

> [[1]]

> [1] 1

>

> [[2]]

> [1] 2

>

> [[3]]

> [1] 3

class(py$D1)

> [1] "list"

py$D2

> [[1]]

> [1] 1 2

>

> [[2]]

> [1] 2

>

> [[3]]

> [1] 3

class(py$D2)

> [1] "list"

py$E

> [[1]]

> [1] 1

>

> [[2]]

> [1] 2

>

> [[3]]

> [1] 3

class(py$E)

> [1] "list"

py$FF

> [[1]]

> [[1]][[1]]

> [1] 1

>

> [[1]][[2]]

> [1] 2

>

>

> [[2]]

> [[2]][[1]]

> [1] 3

>

> [[2]][[2]]

> [1] 4

class(py$FF)

> [1] "list"

py$G

> [[1]]

> [[1]][[1]]

> [1] 1

>

> [[1]][[2]]

> [1] 2

>

>

> [[2]]

> [1] 3

>

> [[3]]

> [1] 4

class(py$G)

> [1] "list"

py$H

> $a

> [1] 1 2 3

>

> $b

> [1] 2 3 4

class(py$H)

> [1] "list"

py$I

> $a

> [1] 1

>

> $b

> [1] 2 3 4

class(py$I)

> [1] "list"

py$J

>

class(py$J)

> [1] "python.builtin.function" "python.builtin.object"

py$J(2, 3)

> [1] 5

4、在 R 中执行 Python 脚本

假设 Python 脚本为 test_pyscript.py,内容见下:

# 打印一些数据

for i in range(10):

print("hello world)

# 定义1个函数

def sum_two_value(a, b):

return a + b

在 R 中执行 test_pyscript.py

source_python("./test_pyscript.py")

> hello world

> hello world

> hello world

> hello world

> hello world

> hello world

> hello world

> hello world

> hello world

> hello world

sum_two_value(1, 2)

> [1] 3

5、在 R Console 中交互式运行 Rrepl_python () 进入 Python 环境

exit 退出 Python 环境

6、R 中运行 Python 代码片段

py_run_string("a = [1, 2, 3]; b = [2, 3, 4]; c = list(zip(a, b))")

7、其他可以使用 F1 键查询 Python 函数帮助

Rmarkdown 的 Python Cell 中无法选中单个语句执行,只能 Cell 整个执行

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值