This document discusses various techniques for integrating R with other programming languages and ecosystems. It begins by asking what to do after building a model in R, such as rewriting the code, integrating R with other languages, or implementing business logic directly in R. The document then covers options for integrating R at the command line, library level, and for calling R from other languages like C++. It also discusses using R for web applications via techniques like rApache, shiny, and openCPU. In the end it argues that R can be effectively used as part of an application infrastructure along with software engineering languages.
8257 interfacing 2 in microprocessor for btech students
R ext world/ useR! Kiev
1. R & EXT. WORD
RESEARCH=>DEVELOPMENT
Ruslan Shevchenko
email: ruslan@shevchenko.kiev.ua
twitter: @rssh1
github: https://github.com/rssh
work: Lynx Capital Partners [consultant]
2. R & EXT. WORLD : TALK OVERIEW
Let we build some model in R.
(Rewrite/Embed/R for all)
Integration techniques.
cmdline [littler, Rscript]
Language-level integration [RInside/Rcpp, RScala, rpy ]
R as net service [rApache, openCPU, Shiny ]
3. LET WE BUILD SOME MODEL IN R.
WHAT NEXT ?
1. Rewrite in ‘real’ programming language for real usage ?
2. Integrate R code with business logic ?
3. Implement business logic in R ?
4. LET WE BUILD SOME MODEL IN R.
WHAT NEXT ?
1. Rewrite in ‘real’ programming language for real usage ?
1. Additional time and money
2. Improvements will follow long path.
=> Only if absolute necessary.
(platform, performance, etc ….)
2. Integrate R code with business logic in other language
3. Implement business logic in R ?
4. Migrate to other ecosystems ?
5. LET WE BUILD SOME MODEL IN R.
WHAT NEXT ?
1. Rewrite in ‘real’ programming language for real usage ?
2. Integrate R code with business logic in other language ?
1. Complex.
2. Extra maintenance cost.
3. Implement business logic in R ?
4. Migrate to other ecosystems ?
=> Only if we have no other way
6. LET WE BUILD SOME MODEL IN R.
WHAT NEXT ?
1. Rewrite in ‘real’ programming language for real usage ?
2. Integrate R code with business logic in other language ?
3. Implement business logic in R ?
1. Esoteric way.
2. R was created without ‘software engineering’ way of
thinking ‘in mind’.
4. Migrate to other ecosystems ?
python
R
Experience
Productivity
Warn: Speculative !!!
(based on filling)
=> Only if R is ideal fit.
7. LET WE BUILD SOME MODEL IN R.
WHAT NEXT ?
1. Rewrite in ‘real’ programming language for real usage ?
2. Integrate R code with business logic in other language ?
3. Implement business logic in R ?
4. Migrate to other ecosystems ? => No clear superior
Now: Python, Octave/Matlab
Future: Scalalab (?)
Julia (?)
//! statistics //! fully compatible/free
8. LET WE BUILD SOME MODEL IN R.
WHAT NEXT ?
1. Rewrite in ‘real’ programming language for real usage ?
2. Integrate R code with business logic ?
3. Implement business logic in R ?
9. LET WE BUILD SOME MODEL IN R.
WHAT NEXT ?
1. Rewrite in ‘real’ programming language for real usage ?
2. Integrate R code with business logic in other language ?
3. Implement business logic in R ?
10. R INTEGRATIONS
(Old S): 1976, New S: 1988, S4: 1996
R: 1995
1-st integrations:
littler: 2006
Rscript: 2006 (in R installation)
Only interactive
12. LIBRARY LEVEL
Embedding in R (C library loaded as R extension)
R extensions. [Cpp as scripting language, etc]
Start something like littler as separate process
Actually used instead R Embedding.
(reasons: Organization of R-interpreter .. )
13. CALL PROCESS/USE FROM R
C++ : RInside (R from C++)/ (RCPP: C++ inside R)
#include <RInside.h>
int main(int argc, char *argv[]) {
RInside R(argc, argv);
R["txt"] = "Hello, world!n";
R.parseEvalQ("cat(txt)");
exit(0);
}
R instance started
// [[Rcpp::export]]
double parallelVectorSum(NumericVector x) {
// declare the SumBody instance
Sum sum(x);
// call parallel_reduce to start the work
parallelReduce(0, x.length(), sum);
// return the computed sum
return sum.value;
}
14. CALL PROCESS/USE FROM R
C++
RInside (R from C++)/ (RCPP: C++ inside R)
Java/Scala
JVMRI | rScala / rJava
Python:
rpy2 / rpython
Yet one approach: // FastR: R implementation in Java
15. NETWORK[WEB]
Low level: rApache / httpuv
rApache: http://rapache.org : R calls from apache
module.
httpuv: https://github.com/rstudio/httpuv/ : web
server inside R
Usually httpuv used for development, rApache - for
serving in production.
16. R & WEB
High-Level:
API: openCPU (http://www.opencpu.org)
Applications:
shiny: http://www.rstudio.com/products/Shiny/
Rook + dashboard (CPAN)
Interactive r from web: RStudio Server
17. OPENCPU
install.packages(‘opencpu’)
library(‘opencpu’) Start web server
http://localhost:2347/ocpu
Browse any data
Call any function
• GET:
• http://<base>/library/datasets/data/cars/json
• cars dataset in json
• http://<base>/library/stats/info
• info about stats package
• http://<base>/library/stats/R/glm/print
• R source for glm
18. OPENCPU POST: DEMO
POST
• URL: http://<base>/library/stats/R/rnorm
• Params: n=10
POST
• URL: http://<base>/library/stats/R/rnorm?json
• Params: n=10
• Result: [-0.315, 0.6241, 0.7175, 1.1813,
-2.5993, -0.9768, -0.034, 0.503,
-0.4165, 1.0353]
Id of object in R environment
19. OPENCPU POST: DEMO
POST
• URL: http://<base>/library/graphics/R/plot
• Params: x=x075fecda05 (key of object received in plot)
http://<base>/tmp/x01ccbd847f/graphics/1/png
opencpu.js — support library
20. OPENCPU
Input/Output format can be set in URL.
Data: JSON, CSV, TAB, Protobuf, RDA, …
Graphics: PNG, SVG, PDF
Texts: plain, markdown,
21. OPENCPU
Web application is R package
(i.e. simple archive with R code and html landing page)
openCPU-server for production. (using rApache )
Exists openCPU PAAS.
Packages which published on github are loaded
automatically
On you local server you can do the same with own repo
22. OPENCPU
Natural way to publish R API
Ideal as ‘R Microservice’.
Caveats:
authorization must be implemented separately
load-balancing must route same sessions to the
same servers.
23. SHINY
Present data from R
http://shiny.rstudio.com/
DSL for HTML UI Elements
Reactive connection with R over web sockets.
// demonstration: movie database
24. BATCH PROCESSING
Triggering Mail, SOAP, file upload, etc.
RSB: R Servise Bus:
http://www.openanalytics.eu/
https://github.com/openanalytics/RSB
Revolution: R on Azure
SparkR (now merged into Spark): R on spark.
25. WHEN TO STOP
When to stop and migrate to other solutions ?
complex integration (more than <K> calls )
hight maintenance cost of hybrid solution.
(rare)
performance loss is issue.
26. R & EXT. WORLD.
It is possible to use ‘R’ as element of application
infrastructure in combination with ‘software-
engineering languages’.
Fast [Research => Deploy] loops is important.