In [1]:
versioninfo()
Julia Version 1.0.1
Commit 0d713926f8 (2018-09-29 19:05 UTC)
Platform Info:
  OS: Windows (x86_64-w64-mingw32)
  CPU: Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, haswell)
Environment:
  JULIA_CMDSTAN_HOME = C:\CmdStan
  JULIA_NUM_THREADS = 4
  JULIA_PKGDIR = C:\JuliaPkg

RCall.jlの使用例

In [2]:
#= To Install RCall:
  using Pkg
  Pkg.add("RCall")
=#
using RCall
In [3]:
X = randn(3,2)
b = reshape([2.0, 3.0], 2,1)
y = X * b + randn(3,1)
Out[3]:
3×1 Array{Float64,2}:
 -4.201556132429842 
  0.4872545860474999
 -1.0317679215996876
In [4]:
# Note that X and y are julia variables. @rput sends y and X to R with the same names
@rput y
@rput X
Out[4]:
3×2 Array{Float64,2}:
 -2.01802   0.478014
 -0.630687  0.394634
 -0.597286  0.372121
In [5]:
# Fit a model in R
R"mod <- lm(y ~ X-1)"
R"summary(mod)"
Out[5]:
RObject{VecSxp}

Call:
lm(formula = y ~ X - 1)

Residuals:
        1         2         3 
 0.001616  0.739294 -0.786096 

Coefficients:
   Estimate Std. Error t value Pr(>|t|)
X1    3.108      1.148   2.707    0.225
X2    4.329      3.490   1.240    0.432

Residual standard error: 1.079 on 1 degrees of freedom
Multiple R-squared:  0.9386,	Adjusted R-squared:  0.8157 
F-statistic: 7.639 on 2 and 1 DF,  p-value: 0.2479

In [6]:
# If you want to retrieve a variable from R, look at this example
R"z <- y * 3"
@rget z
z
Out[6]:
3×1 Array{Float64,2}:
 -12.604668397289526 
   1.4617637581424996
  -3.0953037647990627
In [7]:
# In this example, we really need to use reval
R_lm(some_str_expr) = reval(rparse("lm(" * some_str_expr * ")"))
R_summary = R"summary"
R_mod = R_lm("y ~ X-1")
R_summary(R_mod)
Out[7]:
RObject{VecSxp}

Call:
lm(formula = y ~ X - 1)

Residuals:
        1         2         3 
 0.001616  0.739294 -0.786096 

Coefficients:
   Estimate Std. Error t value Pr(>|t|)
X1    3.108      1.148   2.707    0.225
X2    4.329      3.490   1.240    0.432

Residual standard error: 1.079 on 1 degrees of freedom
Multiple R-squared:  0.9386,	Adjusted R-squared:  0.8157 
F-statistic: 7.639 on 2 and 1 DF,  p-value: 0.2479

In [8]:
# Finally, plotting. Note that if you don't set ylab and xlab, you'll get a very messy plot...
R_plot = R"plot"
R_plot(x=rand(100),y=rand(100),pch=20,cex=2,fg="grey",bty="n",ylab="",xlab="")
R"plot(X[,1],y)"
# Now, you can call R_plot in julia quite like you call plot in R
Out[8]:
RObject{NilSxp}
NULL

Rによるプロット

In [9]:
R"plot(rnorm(2000))"
Out[9]:
RObject{NilSxp}
NULL
In [10]:
R_plot = R"plot";
R_plot(x=randn(2000),ylab="")
Out[10]:
RObject{NilSxp}
NULL
In [11]:
R"""
library(ggplot2)
str(iris)
"""
'data.frame':	150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
Out[11]:
RObject{NilSxp}
NULL
In [12]:
# http://motw.mods.jp/R/ggplot_facet.html

R"""
p <- ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width))
p + geom_point(colour="gray50", size=3) + geom_point(aes(colour=Species))
"""
Out[12]:
RObject{VecSxp}
In [13]:
# http://motw.mods.jp/R/ggplot_geom_histogram.html

R"""
p <- ggplot(iris, aes(x=Sepal.Length))
p + geom_histogram(aes(colour=Species),bins=30)
"""
Out[13]:
RObject{VecSxp}
In [14]:
R"""
p + geom_histogram(aes(fill=Species),bins=30)
"""
Out[14]:
RObject{VecSxp}
In [15]:
R"""
str(diamonds)
"""
Classes 'tbl_df', 'tbl' and 'data.frame':	53940 obs. of  10 variables:
 $ carat  : num  0.23 0.21 0.23 0.29 0.31 0.24 0.24 0.26 0.22 0.23 ...
 $ cut    : Ord.factor w/ 5 levels "Fair"<"Good"<..: 5 4 2 4 2 3 3 3 1 3 ...
 $ color  : Ord.factor w/ 7 levels "D"<"E"<"F"<"G"<..: 2 2 2 6 7 7 6 5 2 5 ...
 $ clarity: Ord.factor w/ 8 levels "I1"<"SI2"<"SI1"<..: 2 3 5 4 2 6 7 3 4 5 ...
 $ depth  : num  61.5 59.8 56.9 62.4 63.3 62.8 62.3 61.9 65.1 59.4 ...
 $ table  : num  55 61 65 58 58 57 57 55 61 61 ...
 $ price  : int  326 326 327 334 335 336 336 337 337 338 ...
 $ x      : num  3.95 3.89 4.05 4.2 4.34 3.94 3.95 4.07 3.87 4 ...
 $ y      : num  3.98 3.84 4.07 4.23 4.35 3.96 3.98 4.11 3.78 4.05 ...
 $ z      : num  2.43 2.31 2.31 2.63 2.75 2.48 2.47 2.53 2.49 2.39 ...
Out[15]:
RObject{NilSxp}
NULL
In [16]:
# http://motw.mods.jp/R/ggplot_facet.html

R"""
p <- ggplot(diamonds, aes(x=carat, y=price))
p + geom_point(aes(colour=clarity)) + facet_grid(cut ~ color, margins=TRUE)
"""
Out[16]:
RObject{VecSxp}
In [17]:
# https://mrunadon.github.io/ggplot2/

R"""
g3<-ggplot(iris,aes(x=Sepal.Length)) #確率密度曲線を描いていきたいので、y軸の宣言は不要です。確率密度がy軸になるので。
g3<-g3+geom_density(aes(fill=Species),size=0.5,alpha=0.5) #geom_density()を使います
g3<-g3+xlim(3,10) #x軸は3から10の範囲としましょう。
g3
"""
RParseError: unicode script is not supported

Stacktrace:
 [1] render(::String) at C:\Users\genkuroki\.julia\packages\RCall\Q4n8R\src\render.jl:14
 [2] @R_str(::LineNumberNode, ::Module, ::Any) at C:\Users\genkuroki\.julia\packages\RCall\Q4n8R\src\macros.jl:62
In [18]:
# https://mrunadon.github.io/ggplot2/

R"""
g3<-ggplot(iris,aes(x=Sepal.Length))
g3<-g3+geom_density(aes(fill=Species),size=0.5,alpha=0.5)
g3<-g3+xlim(3,10)
g3
"""
Out[18]:
RObject{VecSxp}

JuliaとRのあいだでのデータのやり取り

In [19]:
R"hoge <- 3"
Out[19]:
RObject{RealSxp}
[1] 3
In [20]:
@rget hoge
Out[20]:
3.0
In [21]:
moge = Float64(pi)
Out[21]:
3.141592653589793
In [22]:
@rput moge
Out[22]:
3.141592653589793
In [23]:
R"moge"
Out[23]:
RObject{RealSxp}
[1] 3.141593
In [24]:
poge = [
    1 2 3;
    4 5 6;
    7 8 9;
]
Out[24]:
3×3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9
In [25]:
@rput poge
Out[25]:
3×3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9
In [26]:
R"poge"
Out[26]:
RObject{IntSxp}
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
In [27]:
pogecopy = deepcopy(poge)
Out[27]:
3×3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9
In [28]:
R"poge <- 2*poge"
Out[28]:
RObject{RealSxp}
     [,1] [,2] [,3]
[1,]    2    4    6
[2,]    8   10   12
[3,]   14   16   18
In [29]:
@rget poge
Out[29]:
3×3 Array{Float64,2}:
  2.0   4.0   6.0
  8.0  10.0  12.0
 14.0  16.0  18.0
In [30]:
poge
Out[30]:
3×3 Array{Float64,2}:
  2.0   4.0   6.0
  8.0  10.0  12.0
 14.0  16.0  18.0
In [31]:
pogecopy
Out[31]:
3×3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9
In [ ]: