Julia is a very fast and intuitive programming language, yet
There are thus far, to my knowledge, two packages which can be used in Julia to call R:
Author said: "It is written entirely in Julia. There is absolutely no "glue" code written in a compiled language like C or C++. As I said, this may not mean much to you unless you have tried to do something like this, in which case it is astonishing."
RCall requires that a recent version of R, at least 3.2.0, be installed. Packages can be there already or can be installed later. In Juliabox, R is already installed. However, only the default libraries are installed, and it is, to my knowledge, not possible to install new libraries on Juliabox. If R has been installed, it is possible to install RCall.jl from within Julia, using
In [18]:
Pkg.add("RCall")
If you have updated the R installation, you may need to rebuild RCall via
In [19]:
Pkg.build("RCall")
Start using RCall using
In [ ]:
using(RCall)
In [2]:
rprint("1+2")
In [4]:
rprint("a=rnorm(3)")
In [3]:
rprint("library(MASS)")
In [6]:
rprint("fit = lm(bwt ~ ., data = birthwt)")
In [7]:
rprint("fit")
the latter being equivalent to
In [8]:
rprint(:fit)
In [9]:
rprint("ls(fit)")
rprint("fit$fitted.values")
In [10]:
rprint("fit\$fitted.values")
Also, " " (double quotes) and ' ' (single quotes) are synonyms in R, but not in Julia. Inside an rprint, use ' ' for character strings.
In [ ]:
rprint("a="ch"")
does not work, contrary to
In [ ]:
rprint("a='ch'")
In [4]:
rprint("plot(1:10,runif(10), xlab='x', ylab='y')")
It also works for ggplot.
The function reval returns, on top of the raw R output, the type of R object that is returned. R types (in Julia) are all of the forme Rcall.XXXSxp. Examples:
In [4]:
reval("fit = lm(bwt ~ ., data = birthwt)")
Out[4]:
In [5]:
RObject(1)
Out[5]:
In [6]:
RObject([1,2,3])
Out[6]:
In [7]:
RObject(1:3)
Out[7]:
However, RObject does not assign the value to an R variable. For example
In [8]:
a = RObject([1,2,3])
rprint(:a)
Instead, one should use @rput, which copies a Julia variable (that can be of relatively "any" Julia type) to an R variable with the same name. The variable is then "run" in R.
In [10]:
a = [1,2,3]
@rput(a)
Out[10]:
In [11]:
rprint(:a)
In [12]:
rcopy("1")
Out[12]:
In [13]:
rcopy("runif(3)")
Out[13]:
In [14]:
rcopy(:a)
Out[14]:
In [15]:
rcopy("1:3")
Out[15]:
In [17]:
rcopy("c(1,2,3)")
Out[17]:
One can force the (Julia) destination type:
In [19]:
rcopy(Array{Int64,1},"c(1,2,3)")
Out[19]:
But not every R type can be converted to a Julia type.
In [20]:
rcopy(:fit)
Out[20]:
We usually do not want the whole object but are interested in a few values, that can be extracted then converted.
In [21]:
rcopy("fit\$fitted.values")
Out[21]:
Similarly to @rput, there is an @rget, that allows to copy an R variable to Julia (with the same name).
In [22]:
reval("b = fit\$fitted.values")
@rget(b)
Out[22]:
In [11]:
function correlation(x::Array{Float64,1},y::Array{Float64,1},method)
@rput(x,y,method)
reval("res = cor.test(x,y,method=method)\$p.value")
@rget(res)
end
Out[11]:
In [16]:
correlation([1.0,2.0,3.0],[1.0,2.0,3.0],"spearman")
Out[16]:
In [20]:
correlation(rand(10),rand(10),"pearson")
Out[20]:
rcall(fun, args) allows to call an R function, passing parameters that are Julia objects, and returning an R object. I don't particularly recommend it, for it is not clear how the Julia objects were converted and I would rather not mix Julia and R objects on a same command line.
In [24]:
X = rand(10)
rcall(:plot, X, sin(5*X), xlab="x", ylab="y")
Out[24]: