{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Calling R, Python and C++ from Julia\n", "\n", "\n", "- Douglas Bates\n", "- U. of Wisconsin - Madison\n", "- github: dmbates\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## In 2012 I started using Julia\n", "\n", "### Good things\n", "- What Viral said: multiple dispatch, JIT method compilation, extensible type system, interesting language constructs.\n", "\n", "### Bad things\n", "- Many things I knew how to do in `R` were not available or needed to be relearned.\n", "- Re-learning takes a while for things like data visualization systems\n", "- `R` packages often provide data sets for illustration/experimentation. As a rule `Julia` packages don't." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The `RCall` package (JuliaStats/RCall.jl)\n", "\n", "- Julia has the `ccall` function. Steven Johnson had written `PyCall`. Avik created `JavaCall`\n", "- So I started writing `RCall`. Most of the recent work has been done by Randy Lai and Simon Byrne.\n", "- Basic approach\n", " - Create Julia immutables to mirror `R`'s `SEXPREC` struct (see [`types.jl`](https://github.com/JuliaStats/RCall.jl/blob/master/src/types.jl) )\n", " - Locate and dlopen `libR` (see [`setup.jl`](https://github.com/JuliaStats/RCall.jl/blob/master/src/setup.jl) )\n", " - Start an embedded `R` process\n", " - Call `R`'s API\n", "- Functions of interest are `reval`, `rcall`, `rcopy`, etc." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " @R_str 2163 bytes Function\n", " @rget 1576 bytes Function\n", " @rimport 1860 bytes Function\n", " @rlibrary 1296 bytes Function\n", " @rput 1542 bytes Function\n", " @rusing 452 bytes Function\n", " @var_str 259 bytes Function\n", " CharSxp 136 bytes DataType\n", " ClosSxp 148 bytes DataType\n", " CplxSxp 136 bytes DataType\n", " IntSxp 136 bytes DataType\n", " LglSxp 136 bytes DataType\n", " NilSxp 112 bytes DataType\n", " RCall 566 KB Module\n", " RObject 168 bytes DataType\n", " RealSxp 136 bytes DataType\n", " StrSxp 136 bytes DataType\n", " Sxp 92 bytes DataType\n", " anyNA 1377 bytes Function\n", " getAttrib 2722 bytes Function\n", " getNames 978 bytes Function\n", " globalEnv 8 bytes RCall.RObject{RCall.EnvSxp}\n", " isFactor 1066 bytes Function\n", " isNA 4223 bytes Function\n", " isOrdered 1067 bytes Function\n", " rcall 3149 bytes Function\n", " rcopy 39 KB Function\n", " reval 6289 bytes Function\n", " rlang 2229 bytes Function\n", " rparse 829 bytes Function\n", " rprint 4106 bytes Function\n", " setAttrib! 4453 bytes Function\n", " setNames! 1054 bytes Function\n" ] } ], "source": [ "using RCall # make the package available\n", "whos(RCall)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "search: rcall RCall remotecall remotecall_wait remotecall_fetch UniformScaling\n", "\n" ] }, { "data": { "text/latex": [ "Evaluate a function in the global environment. The first argument corresponds to the function to be called. It can be either a FunctionSxp type, a SymSxp or a Symbol.\n" ], "text/markdown": [ "Evaluate a function in the global environment. The first argument corresponds to the function to be called. It can be either a FunctionSxp type, a SymSxp or a Symbol.\n" ], "text/plain": [ "Evaluate a function in the global environment. The first argument corresponds to the function to be called. It can be either a FunctionSxp type, a SymSxp or a Symbol.\n" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?rcall" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "search: rcopy precompile __precompile__ readchomp ProcessGroup\n", "\n" ] }, { "data": { "text/latex": [ "Evaluate and convert the result of a string as an R expression.\n", "\\texttt{rcopy} copies the contents of an R object into a corresponding canonical Julia type.\n", "\\texttt{rcopy(T,p)} converts a pointer \\texttt{p} to a Sxp object to a native Julia object of type T.\n", "\\texttt{rcopy(p)} performs a default conversion.\n" ], "text/markdown": [ "Evaluate and convert the result of a string as an R expression.\n", "\n", "`rcopy` copies the contents of an R object into a corresponding canonical Julia type.\n", "\n", "`rcopy(T,p)` converts a pointer `p` to a Sxp object to a native Julia object of type T.\n", "\n", "`rcopy(p)` performs a default conversion.\n" ], "text/plain": [ "Evaluate and convert the result of a string as an R expression.\n", "\n", "`rcopy` copies the contents of an R object into a corresponding canonical Julia type.\n", "\n", "`rcopy(T,p)` converts a pointer `p` to a Sxp object to a native Julia object of type T.\n", "\n", "`rcopy(p)` performs a default conversion.\n" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "?rcopy" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
1 | 21.0 | 6.0 | 160.0 | 110.0 | 3.9 | 2.62 | 16.46 | 0.0 | 1.0 | 4.0 | 4.0 |
2 | 21.0 | 6.0 | 160.0 | 110.0 | 3.9 | 2.875 | 17.02 | 0.0 | 1.0 | 4.0 | 4.0 |
3 | 22.8 | 4.0 | 108.0 | 93.0 | 3.85 | 2.32 | 18.61 | 1.0 | 1.0 | 4.0 | 1.0 |
4 | 21.4 | 6.0 | 258.0 | 110.0 | 3.08 | 3.215 | 19.44 | 1.0 | 0.0 | 3.0 | 1.0 |
5 | 18.7 | 8.0 | 360.0 | 175.0 | 3.15 | 3.44 | 17.02 | 0.0 | 0.0 | 3.0 | 2.0 |
6 | 18.1 | 6.0 | 225.0 | 105.0 | 2.76 | 3.46 | 20.22 | 1.0 | 0.0 | 3.0 | 1.0 |
7 | 14.3 | 8.0 | 360.0 | 245.0 | 3.21 | 3.57 | 15.84 | 0.0 | 0.0 | 3.0 | 4.0 |
8 | 24.4 | 4.0 | 146.7 | 62.0 | 3.69 | 3.19 | 20.0 | 1.0 | 0.0 | 4.0 | 2.0 |
9 | 22.8 | 4.0 | 140.8 | 95.0 | 3.92 | 3.15 | 22.9 | 1.0 | 0.0 | 4.0 | 2.0 |
10 | 19.2 | 6.0 | 167.6 | 123.0 | 3.92 | 3.44 | 18.3 | 1.0 | 0.0 | 4.0 | 4.0 |
11 | 17.8 | 6.0 | 167.6 | 123.0 | 3.92 | 3.44 | 18.9 | 1.0 | 0.0 | 4.0 | 4.0 |
12 | 16.4 | 8.0 | 275.8 | 180.0 | 3.07 | 4.07 | 17.4 | 0.0 | 0.0 | 3.0 | 3.0 |
13 | 17.3 | 8.0 | 275.8 | 180.0 | 3.07 | 3.73 | 17.6 | 0.0 | 0.0 | 3.0 | 3.0 |
14 | 15.2 | 8.0 | 275.8 | 180.0 | 3.07 | 3.78 | 18.0 | 0.0 | 0.0 | 3.0 | 3.0 |
15 | 10.4 | 8.0 | 472.0 | 205.0 | 2.93 | 5.25 | 17.98 | 0.0 | 0.0 | 3.0 | 4.0 |
16 | 10.4 | 8.0 | 460.0 | 215.0 | 3.0 | 5.424 | 17.82 | 0.0 | 0.0 | 3.0 | 4.0 |
17 | 14.7 | 8.0 | 440.0 | 230.0 | 3.23 | 5.345 | 17.42 | 0.0 | 0.0 | 3.0 | 4.0 |
18 | 32.4 | 4.0 | 78.7 | 66.0 | 4.08 | 2.2 | 19.47 | 1.0 | 1.0 | 4.0 | 1.0 |
19 | 30.4 | 4.0 | 75.7 | 52.0 | 4.93 | 1.615 | 18.52 | 1.0 | 1.0 | 4.0 | 2.0 |
20 | 33.9 | 4.0 | 71.1 | 65.0 | 4.22 | 1.835 | 19.9 | 1.0 | 1.0 | 4.0 | 1.0 |
21 | 21.5 | 4.0 | 120.1 | 97.0 | 3.7 | 2.465 | 20.01 | 1.0 | 0.0 | 3.0 | 1.0 |
22 | 15.5 | 8.0 | 318.0 | 150.0 | 2.76 | 3.52 | 16.87 | 0.0 | 0.0 | 3.0 | 2.0 |
23 | 15.2 | 8.0 | 304.0 | 150.0 | 3.15 | 3.435 | 17.3 | 0.0 | 0.0 | 3.0 | 2.0 |
24 | 13.3 | 8.0 | 350.0 | 245.0 | 3.73 | 3.84 | 15.41 | 0.0 | 0.0 | 3.0 | 4.0 |
25 | 19.2 | 8.0 | 400.0 | 175.0 | 3.08 | 3.845 | 17.05 | 0.0 | 0.0 | 3.0 | 2.0 |
26 | 27.3 | 4.0 | 79.0 | 66.0 | 4.08 | 1.935 | 18.9 | 1.0 | 1.0 | 4.0 | 1.0 |
27 | 26.0 | 4.0 | 120.3 | 91.0 | 4.43 | 2.14 | 16.7 | 0.0 | 1.0 | 5.0 | 2.0 |
28 | 30.4 | 4.0 | 95.1 | 113.0 | 3.77 | 1.513 | 16.9 | 1.0 | 1.0 | 5.0 | 2.0 |
29 | 15.8 | 8.0 | 351.0 | 264.0 | 4.22 | 3.17 | 14.5 | 0.0 | 1.0 | 5.0 | 4.0 |
30 | 19.7 | 6.0 | 145.0 | 175.0 | 3.62 | 2.77 | 15.5 | 0.0 | 1.0 | 5.0 | 6.0 |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
\n", " | br1 | \n", "br2 | \n", "
---|---|---|
0 | \n", "0.000000 | \n", "0.000000 | \n", "
1 | \n", "-0.773834 | \n", "-0.006075 | \n", "
2 | \n", "-0.537581 | \n", "-0.281177 | \n", "
3 | \n", "0.070239 | \n", "0.989620 | \n", "
4 | \n", "0.913922 | \n", "0.272461 | \n", "
5 | \n", "0.783253 | \n", "0.943957 | \n", "
6 | \n", "1.847161 | \n", "0.796048 | \n", "
7 | \n", "0.448132 | \n", "1.478668 | \n", "
8 | \n", "-2.358731 | \n", "0.106409 | \n", "
9 | \n", "-3.037559 | \n", "-0.322209 | \n", "
10 | \n", "-0.647172 | \n", "-0.024637 | \n", "
11 | \n", "-0.324446 | \n", "0.887330 | \n", "
12 | \n", "-1.087391 | \n", "0.764816 | \n", "
13 | \n", "-0.092598 | \n", "1.635592 | \n", "
14 | \n", "0.270325 | \n", "1.103798 | \n", "
15 | \n", "0.026771 | \n", "0.601902 | \n", "
16 | \n", "-1.469982 | \n", "1.815822 | \n", "
17 | \n", "-2.144328 | \n", "0.957945 | \n", "
18 | \n", "-3.344290 | \n", "1.105669 | \n", "
19 | \n", "-2.112758 | \n", "1.303400 | \n", "
20 | \n", "-3.072021 | \n", "0.911289 | \n", "
21 | \n", "-2.243848 | \n", "1.488212 | \n", "
22 | \n", "-1.276259 | \n", "1.990742 | \n", "
23 | \n", "-2.105055 | \n", "2.864032 | \n", "
24 | \n", "-1.909855 | \n", "4.591759 | \n", "
25 | \n", "-2.241574 | \n", "4.498321 | \n", "
26 | \n", "-2.712888 | \n", "4.342676 | \n", "
27 | \n", "-2.501568 | \n", "4.245411 | \n", "
28 | \n", "-2.737541 | \n", "3.471929 | \n", "
29 | \n", "-1.666890 | \n", "3.263433 | \n", "
... | \n", "... | \n", "... | \n", "
9971 | \n", "-67.201469 | \n", "-37.090684 | \n", "
9972 | \n", "-68.372847 | \n", "-37.603236 | \n", "
9973 | \n", "-69.053534 | \n", "-37.842471 | \n", "
9974 | \n", "-67.648497 | \n", "-38.373551 | \n", "
9975 | \n", "-65.773006 | \n", "-39.242039 | \n", "
9976 | \n", "-66.520155 | \n", "-39.173315 | \n", "
9977 | \n", "-66.774692 | \n", "-40.773012 | \n", "
9978 | \n", "-67.839113 | \n", "-39.828624 | \n", "
9979 | \n", "-68.676012 | \n", "-40.120377 | \n", "
9980 | \n", "-68.364612 | \n", "-38.445496 | \n", "
9981 | \n", "-66.843212 | \n", "-38.913253 | \n", "
9982 | \n", "-66.390980 | \n", "-38.201632 | \n", "
9983 | \n", "-65.733951 | \n", "-37.470297 | \n", "
9984 | \n", "-65.099306 | \n", "-38.119241 | \n", "
9985 | \n", "-66.354152 | \n", "-37.442763 | \n", "
9986 | \n", "-68.651218 | \n", "-38.013503 | \n", "
9987 | \n", "-67.823224 | \n", "-37.741410 | \n", "
9988 | \n", "-68.053629 | \n", "-38.639877 | \n", "
9989 | \n", "-67.943860 | \n", "-36.262430 | \n", "
9990 | \n", "-66.721974 | \n", "-35.792575 | \n", "
9991 | \n", "-67.957720 | \n", "-35.626207 | \n", "
9992 | \n", "-68.531987 | \n", "-34.892304 | \n", "
9993 | \n", "-68.036708 | \n", "-34.475120 | \n", "
9994 | \n", "-69.109629 | \n", "-34.392904 | \n", "
9995 | \n", "-69.369302 | \n", "-34.677581 | \n", "
9996 | \n", "-69.953834 | \n", "-36.628048 | \n", "
9997 | \n", "-69.380250 | \n", "-37.220133 | \n", "
9998 | \n", "-69.382725 | \n", "-36.422765 | \n", "
9999 | \n", "-69.528264 | \n", "-35.077929 | \n", "
10000 | \n", "-68.787819 | \n", "-35.410307 | \n", "
10001 rows × 2 columns
\n", "