Calling R Externally : from command line, Python, Java, and C++



Similar documents
Introduction to Web Development with R

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Science Gateway Services for NERSC Users

The power of IBM SPSS Statistics and R together

Introduction to Python

Using DeployR to Solve the R Integration Problem

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

CSC 120: Computer Science for the Sciences (R section)

Analytic Modeling in Python

Lecture 8. Online GIS

Java the UML Way: Integrating Object-Oriented Design and Programming

Rserve A Fast Way to Provide R Functionality to Applications

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

Sources: On the Web: Slides will be available on:

Porting VNC to Mobile Platforms

Moving from CS 61A Scheme to CS 61B Java

MatrixSSL Getting Started

Computational Mathematics with Python

irods and Metadata survey Version 0.1 Date March Abhijeet Kodgire 25th

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

Braindumps.C questions

Chapter 1. Introduction to ios Development. Objectives: Touch on the history of ios and the devices that support this operating system.

Credits: Some of the slides are based on material adapted from

Crash Course in Java

.NET and J2EE Intro to Software Engineering

GUI and Web Programming

Integrating R with the Go programming language using interprocess communication

Computational Mathematics with Python

Java Application Developer Certificate Program Competencies

WEB SITE DEVELOPMENT WORKSHEET

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

VIRTUAL LABORATORY: MULTI-STYLE CODE EDITOR

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

DISCOVERY OF WEB-APPLICATION VULNERABILITIES USING FUZZING TECHNIQUES

Assignment 2: More MapReduce with Hadoop

Kerberos + Android. A Tale of Opportunity. Slide 1 / 39. Copyright 2012 yassl

Networks and Services

Consuming and Producing Web Services with WST and JST. Christopher M. Judd. President/Consultant Judd Solutions, LLC

TIBCO Runtime Agent Authentication API User s Guide. Software Release November 2012

Developing a Web Server Platform with SAPI Support for AJAX RPC using JSON

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

C Programming Review & Productivity Tools

KonyOne Server Prerequisites _ MS SQL Server

Lecture 1 Introduction to Android

CIS 192: Lecture 13 Scientific Computing and Unit Testing

Visualisation in the Google Cloud

Getting Started with the Internet Communications Engine

AC : MATHEMATICAL MODELING AND SIMULATION US- ING LABVIEW AND LABVIEW MATHSCRIPT

Fundamentals of Java Programming

1Intro. Apache is an open source HTTP web server for Unix, Apache

Computational Mathematics with Python

MapReduce. MapReduce and SQL Injections. CS 3200 Final Lecture. Introduction. MapReduce. Programming Model. Example

3. Broken Account and Session Management. 4. Cross-Site Scripting (XSS) Flaws. Web browsers execute code sent from websites. Account Management

Java with Eclipse: Setup & Getting Started

A Tool for Evaluation and Optimization of Web Application Performance

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Predicting outcome of soccer matches using machine learning

The full setup includes the server itself, the server control panel, Firebird Database Server, and three sample applications with source code.

McAfee Cloud Identity Manager

Evolution of the Major Programming Languages

Object systems available in R. Why use classes? Information hiding. Statistics 771. R Object Systems Managing R Projects Creating R Packages

Source Code Review Using Static Analysis Tools

HPC Wales Skills Academy Course Catalogue 2015

CS 558 Internet Systems and Technologies

Web Applications Testing

ETPL Extract, Transform, Predict and Load

CSC 551: Web Programming. Spring 2004

Division of Informatics, University of Edinburgh

Web Development with R

Magento Search Extension TECHNICAL DOCUMENTATION

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

INTERNET PROGRAMMING AND DEVELOPMENT AEC LEA.BN Course Descriptions & Outcome Competency

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Graphical Environment Tool for Development versus Non Graphical Development Tool

Automatic Text Analysis Using Drupal

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

XML Processing and Web Services. Chapter 17

Kopano product strategy & roadmap

Comparison of Triple Stores

Introduction to Python

Chapter 17. Transport-Level Security

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

About This Document 3. Integration and Automation Capabilities 4. Command-Line Interface (CLI) 8. API RPC Protocol 9.

Government Girls Polytechnic, Bilaspur

Syllabus INFO-UB Design and Development of Web and Mobile Applications (Especially for Start Ups)

Distributed R for Big Data

Wireless Security Camera with the Arduino Yun

Internet Information TE Services 5.0. Training Division, NIC New Delhi

Real Time Market Data and Trade Execution with R

Beyond The Web Drupal Meets The Desktop (And Mobile) Justin Miller Code Sorcery Workshop, LLC

13 File Output and Input

2. Distributed Handwriting Recognition. Abstract. 1. Introduction

Introduction Our choice Example Problem Final slide :-) Python + FEM. Introduction to SFE. Robert Cimrman

twilio-salesforce Documentation

Programming Languages & Tools

DATA SCIENCE CURRICULUM WEEK 1 ONLINE PRE-WORK INSTALLING PACKAGES COMMAND LINE CODE EDITOR PYTHON STATISTICS PROJECT O5 PROJECT O3 PROJECT O2

Transcription:

Calling R Externally : from command line, Python, Java, and C++ Leo Pekelis February 2nd, 2013, Bicoastal Datafest, Stanford University 1/11

What is R? Why and why not use it? R is a language and environment for statistical computing and graphics R provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, ) and graphical techniques, and is highly extensible. " http://www.r-project.org/ R s main selling point is the massive amount of libraries allowing you to perform almost any statistical procedure in a single command There are currently 4273 available packages on CRAN, all independently tested, and generally peer reviewed. R is great for performing analysis on a dataset, and presenting findings in a static set of graphics R is not so great for: Writing dynamic API, such as a web app High end graphics looping and iteration. This is devilishly slow in R, and in fact, most iterative algorithms call C externally from inside R. Programmers who do not want to learn the eccentricities of a new language 2/11

Do you want a crash course on R? Quick-R, http://www.statmethods.net/ A useful website with concise explanations of some of the basic (and more advanced) features of R. Designed to get you running statistical analysis quickly, and painlessly. Sources to search for answering specific R related questions 1. R (my question) into Google 2. http://stackoverflow.com/ 3. R mailing list archive - http://tolstoy.newcastle.edu.au/r/ 4. The documentation for any function can be called by?function (e.g.?mean). One of the top links in google will also generally return this. 3/11

Calling R from (your favorite programming language). Command Line If you can read from and write to the command line, a few simple commands can run R scripts and extract the output. Python Java C++ The package rpy2 provides low-level interface to R, and access to all R libraries. http://rpy.sourceforge.net/rpy2.html JRI is a Java/R Interface, which allows to run R inside Java applications as a single thread. Basically it loads R dynamic library into Java and provides a Java API to R functionality. It supports both simple calls to R functions and a full running REPL (read-eval-print loop). http://www.rforge.net/jri/ Rcpp provides matching C++ classes for a large number of basic R data types. Hence, a package author can keep his data in normal R data structures without having to worry about translation or transfering to C++. the data structures can be accessed as easily at the C++ level, and used in the normal manner. http://dirk.eddelbuettel.com/code/rcpp.html RInside C++ classes to embed R in C++ applications. The RInside packages makes it easier to have R inside your C++ application by providing a C++ wrapper class providing the R interpreter. Makes use of Rcpp http://dirk.eddelbuettel.com/code/rinside.html 4/11

5/11

What s to come The following will give more details on using each of the solutions defined in the previous slide. I do not pretend to be an expert. The man and help pages will be your friends. 6/11

Running R from Command Line General command line syntax, R [options] [<infile] [>outfile] Required & Useful Options --vanilla, evokes R such that it doesn t save data upon exiting (required), and doesn t start with any user profile information --max-ppsize=n, max size of pointer protection stack. Can be increased up to 100,000 for large and complicated computations --args [arg1] [arg2]..., can specify input arguments after with spaces in between The arguments are then returned as a character vector by commandargs(true)within R An example may be something like: R --vanilla '--args 1 10 TRUE' < myscript.r > outfile.txt Note outfile.txtwill get the console results of running the code in myscript.r To have external access to data structures created while running the script incorporate write.csv()or save()into myscript.r 7/11

Running R from Python: rpy2 rpy2 interacts with R in a number of different ways high level interface - designed to facilitate the use of R by Python programmers. R objects are exposed as instances of Pythonimplemented classes, with R functions as bound methods to those objects in a number of cases. robjects.rclass instances one R session on loading the module. Then Python commands interact with the R session, sending commands, and extracting varables. e.g. robjects.globalenv['foo'] = 1.2- stores the value 1.2 in a new variable fooin R foo = robjects.r['foo']- extracts the variable foofrom R into a Python variable of the same name foo[0]- call to the first element of Python foo, prints 1.2 Sending commands to R is handled similarly. read-eval-print loop (REPL), where interactivity is important. numpy - a signature numerical package in Python can share objects with R through a subpackage of rpy2 low level interface - similar to R s C level API. Used for performance optimization or in developing high level interactions. Full documentation: http://rpy.sourceforge.net/rpy2/doc- 2.3/html/index.html 8/11

Running R from JAVA: JRI Start an R instance with Rengine re=new Rengine(args, false, new TextConsole()); TextConsole()is a class which implements R callbacks. From looking at examples, it needs to be defined manually. Then R commands are evalauted with re.eval("r command")and the output is the result returned by R e.g. Load dataset irisand store it in a JAVA subclass for representing R objects REXP x; re.eval("data(iris)",false); System.out.println(x=re.eval("iris")); Documentation is not great, but a couple example files can be found in the tar ball. http://www.rforge.net/jri/files/ 9/11

Running R from C++: RCpp and Rinside In RCpp, as before, R data types have dedicated classes e.g. Rcpp::NumericVectorclass for (you guessed it) numeric vectors in R Main use case is: existing R code may be replaced byequivalent C++ code in order to reap performance gains. But can still call R code from C++. The following example samples 10 random normals with sd=100 Environment stats("package:stats"); Function rnorm = stats["rnorm"]; return rnorm(10,named("sd", 100.0)); Rinside - not a lot of documentation, but the source does come with 6 examples including a light web-app using the Wt toolkit 10/11

Webapps with R If you are ever interested in creating a webapp with R, there are at least a few approaches. 1. Shiny allows for easy web applications using only R http://rstudio.github.com/shiny/tutorial/# 2. Rserve is a TCP/IP server which allows other programs to use facilities of R Client-side implementations are available for popular languages such as C/C++, PHP and Java. Rserve supports remote connection, authentication and file transfer. Typical use is to integrate R backend for computation of statstical models, plots etc. in other applications. http://www.rforge.net/rserve/index.html 3. Wt (pronounced as witty) is a C++ library for developing web applications. The API is widget-centric and uses well-tested patterns of desktop GUI development tailored to the web. To the developer, it offers abstraction of webspecific implementation details, including client-server protocols, event handling, graphics support, graceful degradation (or progressive enhancement), and URL handling. http://www.webtoolkit.eu/wt 11/11