打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Julia编程04:Julia调用R和Python

参考使用homebrew管理linux和mac软件安装homebrew、mamba和Python

Python环境

mamba create -y -n jlpy python=3.10
mamba activate jlpy
mamba install matplotlib
which python
# /opt/homebrew/Caskroom/mambaforge/base/envs/jlpy/bin/python

R环境

在R中运行,找到R_HOME

R.home()
# /Library/Frameworks/R.framework/Resources

Build

在Julia中运行

using Pkg
Pkg.add(["PyCall","RCall"])
ENV["PYTHON"]="/opt/homebrew/Caskroom/mambaforge/base/envs/jlpy/bin/python"
Pkg.build("PyCall")

ENV["R_HOME"] = "/Library/Frameworks/R.framework/Resources"
Pkg.build("RCall")

调用R

还有许多用法,可以参考官网

using RCall
@rimport base as rbase
rbase.sum([123])
# RObject{IntSxp}
# [1] 6

跟在R中写代码已经没什么区别了

R"
library(ggplot2)
qplot(iris$Sepal.Length,iris$Sepal.Width,color=iris$Species)
ggsave('iris.png')
"

调用Python

还有许多用法,可以参考官网

using PyCall
math = pyimport("math")
math.sin(math.pi / 4# returns ≈ 1/√2 = 0.70710678...
plt = pyimport("matplotlib.pyplot")
x = range(0;stop=2*pi,length=1000); y = sin.(3*x + 4*cos.(2*x));
plt.plot(x, y, color="blue", linewidth=2.0, linestyle="--")
plt.show()

Reference

https://juliainterop.github.io/RCall.jl/stable/
https://github.com/JuliaPy/PyCall.jl


Julia is a high-level, dynamic programming language. Its features are well suited for numerical analysis and computational science. Distinctive aspects of Julia's design include a type system with parametric polymorphism in a dynamic programming language; with multiple dispatch as its core programming paradigm.
The most significant departures of Julia from typical dynamic languages are:
The core language imposes very little; Julia Base and the standard library are written in Julia itself, including primitive operations like integer arithmetic
A rich language of types for constructing and describing objects, that can also optionally be used to make type declarations
The ability to define function behavior across many combinations of argument types via multiple dispatch
Automatic generation of efficient, specialized code for different argument types
Good performance, approaching that of statically-compiled languages like C
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Julia语言初体验
为什么说 Julia 更优于 Python?
A Complete Tutorial to Learn Data Science with Jul...
在你的 Android 手机上创建和运行 Python 应用 | Linux 中国
用ExcelPython在Excel中调用Python
C调用Python的函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服