Mixing R with
- ther languages
JOHN D. COOK, PHD SINGULAR VALUE CONSULTING
Mixing R with other languages JOHN D. COOK, PHD SINGULAR VALUE - - PowerPoint PPT Presentation
Mixing R with other languages JOHN D. COOK, PHD SINGULAR VALUE CONSULTING Why R? Libraries, libraries, libraries De facto standard for statistical research Nice language, as far as statistical languages go Quirky, flawed, and
JOHN D. COOK, PHD SINGULAR VALUE CONSULTING
Libraries, libraries, libraries De facto standard for statistical research Nice language, as far as statistical languages go “Quirky, flawed, and an enormous success.”
Improve performance of R code
Execution speed (e.g. loops) Memory management
Raid R’s libraries
Vectorize Rewrite not using R
Everything is a vector Everything can be null or NA Unit-offset vectors Zero index legal but strange Negative indices remove elements Matrices filled by column by default $ acts like dot, dot not special
Must manage low-level details of R object model and memory Requires Rtools on Windows Lots of macros like REALSXP, PROTECT, and UNPROTECT Use C++ (Rcpp) instead
“I do not recommend using C for writing new high-performance
The most widely used extension method for R Call C, C++, or Fortran from R Companion project RInside to call R from C++ Extensive support even for advanced C++ Create R packages or inline code http://rcpp.org Dirk Eddelbuettel’s book
library(Rcpp) cppFunction('int add(int x, int y, int z) { int sum = x + y + z; return sum; }') add(1, 2, 3)
RDCOM
http://sunsite.univie.ac.at/rcom/
F# type provider for R
http://bluemountaincapital.github.io/FSharpRProvider/
R.NET
https://rdotnet.codeplex.com/
execute sp_execute_external_script @language = N'R' , @script = N' OutputDataSet<- data.frame(c("hello"), " ", c("world"));' , @input_data_1 = N' ' WITH RESULT SETS ( ([col1] varchar(20) , [col2] char(1), [col3] varchar(20) ) );
HaskellR from Tweag.io
http://tweag.github.io/HaskellR/
Use quasi-quoting into inline R
[r| … |]
Interactive REPL with H wrapper around GHCi Works with Jupyter notebooks
Crufty but powerful, like all things Emacs Ships with support for many languages Works reliably cross-platform Good for exploration / prototyping Literate programming
Supported Other ABC Dot Ledger Org Screen Axiom Mathematica Asymptote Ebnf Lilypond Perl Sed Elixir Mathomatic Awk Elisp Lisp Picolisp Shell Eukleides MongoDB C Forth Make PlantUML Shen Fomus Neo4j C++ Fortran Matlab Processing SQL Google translate OZ Calc Gnuplot Maxima Python SQLite Groovy Prolog Clojure Haskell Mscgen R Stan HTML Rec Comint Io OCaml Ruby http request SML Coq J Octave Sass iPython Stata CSS Java Scala Julia Tcl D Javascript Scheme Kotlin Typescript Ditaa LaTeX LFE
Text, images, LaTeX equations, etc. #+begin_src R … #+end_src text etc. … #+begin_src python … #+end_src
#+name: sin_r #+begin_src R :var x=0 sin(x) #+end_src #+name: cos_p #+begin_src python :var x=1 import math return math.cos(x) #+end_src #+name: sum_sq #+begin_src perl :var a=3 :var b=4 $a*$a + $b*$b #+end_src #+call: sum_sq(sin_r(1), cos_p(1)) #+results: : 1
Started out as IPython notebooks Julia + Python + R Multiple languages supported (separately) Less transparent than org-babel
For better: images, formatting, etc. For worse: Hard to version and diff
Bash F# Julia Prolog C Forth Matlab Python C++ Go Maxima Ruby C# Haskell OCaml SAS Clojure Hy Octave SageMath Coffeescript J PHP Scala Common Lisp Java Perl(6) Tcl Erlang Javascript PowerShell Xonsh
A fork of IPython, predecessor to Jupyter http://beakernotebook.com/ Cells can be written in different languages Set attribute on beaker object in one language,
access attribute from another language
R data.frame <-> Python pandas.DataFrame
beaker.foo = “Hello world” # Python cell x <- beaker::get(‘foo’) # R cell beaker::set(‘answer’, 42) # R cell z = beaker.answer[0] # Python cell
C++ Java Python(3) Clojure JavaScript R F# Julia Ruby Groovy Lua/Torch Scala/Spark HTML Node SQL
Similar to Jupyter, Beaker http://rmarkdown.rstudio.com Can mix languages in a single document Exchange data between languages via data frames Many publication export formats
Bash R CSS Rcpp JavaScript SQL Python Stan
Text (markdown)… ```{r} x <- “hello from R” print(x) ``` Text … ```{python} x = “ “.join( [“Hello”, “from”, “Python”] ) print(x) ```
Make R more efficient, or borrow its libraries. R differences: null/NA, vectors, unit offset, etc. Most of these approaches do not simply install and “just work.” Org-babel works as documented, but maybe not as expected. Most general/powerful approach: language <-> Rcpp <-> R