R语言和Python交互

交互原理

借助了reticulate这个包,
其中两个工具的模块包加载方式在R中是import(“math”) ;在Python中是import math
调用模块包后执行结果,执行方式:R中是math$sqrt(20);python中是math.sqrt(20)

python执行结果

在这里插入图片描述

R语言执行结果

在这里插入图片描述

整体code

library(reticulate)
py_available() # 检查python是否存在
os <- import("os") # 调用python的os包
os$getcwd() # 在R中执行python的os包中的命令

math<-import("math") # R语言导入math包
result1<-math$sqrt(20) # R语言中使用Python的math函数包的功能模块,进行计算
print(result1) # 查看调用python包运行的结果

借助了reticulate这个包,其中两个工具的模块包加载方式在R中是import(“math”) ;在Python中是import math
调用模块包后执行结果,执行方式:R中是math$sqrt(20);python中是math.sqrt(20)

猜你喜欢

转载自blog.csdn.net/tandelin/article/details/106466101