V1

2023/04/17阅读：30主题：姹紫

# Julia编程04：Julia调用R和Python

<~生~信~交~流~与~合~作~请~关~注~公~众~号@生信探索>

## Python环境

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

## R环境

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

## Build

``using PkgPkg.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 rbaserbase.sum([1, 2, 3])# RObject{IntSxp}# [1] 6``

``R"library(ggplot2)a=irisqplot(iris\$Sepal.Length,iris\$Sepal.Width,color=iris\$Species)ggsave('iris.png')"``

## 调用Python

``using PyCallmath = 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``

V1