Julia’s younger ecosystem sometimes lacks packages for specialized tasks. The solution: leverage R packages directly from Julia through RCall.jl, enabling seamless interoperability between the two languages.
Installation
] add RCall
Running R Code from Julia
Interactive Mode (REPL)
After importing RCall, access the R REPL by typing $. The prompt switches from julia> to R>, allowing native R commands:
julia>usingRCall# type `$` to enter R modeR> install.packages("ggplot2")R>library(ggplot2)R>data(diamonds)R>ggplot(diamonds, aes(x=carat, y=price)) +geom_point()
Non-Interactive Mode
The @R_str macro executes R code programmatically:
julia> R"y = 2"RObject{RealSxp}[1] 2
Sending Julia Variables to R
Using @rput
julia> x =11julia>@rput x1R> x[1] 1
String Interpolation
julia> x =11julia> R"y = $x"RObject{IntSxp}[1] 1R>1+$x[1] 2