Getting started
The RCall package is loaded via
julia> using RCall
This will initialize the R process in the background.
Several Ways to use RCall
RCall provides multiple ways to allow R interacting with Julia.
R REPL mode
The R REPL mode allows real time switching between the Julia prompt and R prompt. Press $
to activate the R REPL mode and the R prompt will be shown. (Press backspace
to leave R REPL mode in case you did not know.)
julia> foo = 1
1
R> x <- $foo
R> x
[1] 1
The R REPL mode supports variable substitution of Julia objects via the $
symbol. It is also possible to pass Julia expressions in the REPL mode.
R> x = $(rand(10))
R> sum(x)
[1] 5.097083
@rput and @rget macros
These macros transfer variables between R and Julia environments. The copied variable will have the same name as the original.
julia> z = 1
1
julia> @rput z
1
R> z
[1] 1
R> r = 2
julia> @rget r
2.0
julia> r
2.0
It is also possible to put and get multiple variables in one line.
julia> foo = 2
2
julia> bar = 4
4
julia> @rput foo bar
4
R> foo + bar
[1] 6
@R_str string macro
Another way to use RCall is the R""
string macro, it is especially useful in script files.
julia> R"rnorm(10)"
RCall.RObject{RCall.RealSxp}
[1] -0.69686314 0.82501977 0.29769891 1.19460610 0.53069997 0.33870637
[7] -0.54528452 0.19863505 0.09934933 -1.42159204
This evaluates the expression inside the string in R, and returns the result as an RObject
, which is a Julia wrapper type around an R object.
The R""
string macro supports variable substitution of Julia objects via the $
symbol, whenever it is not valid R syntax (i.e. when not directly following a symbol or completed expression such as aa$bb
):
julia> x = randn(10)
10-element Array{Float64,1}:
-1.3968
-1.24043
0.264661
0.404198
-0.590791
-1.38921
0.0411671
-0.219625
1.1252
0.89752
julia> R"t.test($x)"
RCall.RObject{RCall.VecSxp}
One Sample t-test
data: `#JL`$x
t = -0.72025, df = 9, p-value = 0.4897
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-0.8712688 0.4504468
sample estimates:
mean of x
-0.210411
It is also possible to pass Julia expressions which are evaluated before being passed to R: these should be included in parentheses
julia> R"optim(0, $(x -> x-cos(x)), method='BFGS')"
RCall.RObject{RCall.VecSxp}
$par
[1] -1.56343
$value
[1] -1.570796
$counts
function gradient
14 13
$convergence
[1] 0
$message
NULL
A large chunk of code could be quoted between triple string quotations
julia> y = 1
1
julia> R"""
f <- function(x, y) x + y
ret <- f(1, $y)
"""
RCall.RObject{RCall.RealSxp}
[1] 2
RCall API
The reval
function evaluates any given input string as R code in the R environment. The returned result is an RObject
object.
julia> jmtcars = reval("mtcars");
julia> names(jmtcars)
11-element Array{Symbol,1}:
:mpg
:cyl
:disp
:hp
:drat
:wt
:qsec
:vs
:am
:gear
:carb
julia> jmtcars[:mpg]
RCall.RObject{RCall.RealSxp}
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
[31] 15.0 21.4
julia> typeof(jmtcars)
RCall.RObject{RCall.VecSxp}
The rcall
function is used to construct function calls.
julia> rcall(:dim, jmtcars)
RCall.RObject{RCall.IntSxp}
[1] 32 11
The arguments will be implicitly converted to RObject
upon evaluation.
julia> rcall(:sum, Float64[1.0, 4.0, 6.0])
RCall.RObject{RCall.RealSxp}
[1] 11
The rcopy
function converts RObject
s to Julia objects. It uses a variety of heuristics to pick the most appropriate Julia type:
julia> rcopy(R"c(1)")
1.0
julia> rcopy(R"c(1, 2)")
2-element Array{Float64,1}:
1.0
2.0
julia> rcopy(R"list(1, 'zz')")
2-element Array{Any,1}:
1.0
"zz"
julia> rcopy(R"list(a=1, b='zz')")
DataStructures.OrderedDict{Symbol,Any} with 2 entries:
:a => 1.0
:b => "zz"
It is possible to force a specific conversion by passing the output type as the first argument:
julia> rcopy(Array{Int}, R"c(1,2)")
2-element Array{Int64,1}:
1
2
Converters and Constructors could also be used specifically to yield the desired type.
julia> convert(Array{Float64}, R"c(1,2)")
2-element Array{Float64,1}:
1.0
2.0
julia> Float64(R"1+3")
4.0