SlideShare a Scribd company logo
The Compatibility Challenge:
Examining R and Developing TERR
Michael Sannella (msannell@tibco.com)
TIBCO Spotfire
http://spotfire.tibco.com
These slides contain all the content from the
useR!2014 poster with the same name
What is TERR?
• TERR: TIBCO® Enterprise Runtime for R
• A commercial R-compatible statistics engine.
• The latest in a family of scripting engines.
• S, S-PLUS®, R, TERR
• Embedded in Spotfire®
• Provides R scripting, powers analytic tools.
• Free Developer's Edition available.
• Commercially available for custom integration.
2
Why R Compatibility?
• We developed TERR as a completely independent
engine, without looking at R sources or
documentation.
• However, we want TERR to be able to load and run
R packages from CRAN and other repositories, so
TERR must be compatible with R.
• Many CRAN packages work with TERR without change.
• Some CRAN packages can be made to work with TERR if
we rebuild them, without changing any of their code.
• Some (hopefully few) CRAN packages require code
changes to make them work under TERR.
3
How to Achieve R Compatibility?
• Our approach: Examine R’s behavior, write unit
tests, and make TERR pass these tests.
• R compatibility is a moving target: As R and the
CRAN packages change over time, we must update
TERR to follow.
• R Wrinkles: While tracking down TERR
incompatibility issues, we uncovered some
interesting R behavior.
• In some cases, we decided not to make TERR
completely compatible with R.
4
Developing Compatibility Using
Key Packages
• For some elements of R, a good way to develop
compatibility was to focus on a key package or
application that exercises certain functionality.
• Key package: The CRAN Matrix package
• Matrix fully exercised the S4 object system.
• Getting the Matrix tests working gave us confidence that we
had a compatible implementation of S4.
• Key application: The RStudio GUI
• RStudio exercised the R engine entries used to embed R
within an application.
• RStudio exercised source-code access via the “srcref”
attribute on parsed expressions and functions.
• RStudio exercised the trace, browser functions used for
breakpoints and single-stepping.
5
R Wrinkles: Character Encoding
• TERR/R Incompatibility: R uses the "unknown"
character encoding for most strings, whereas TERR
uses the UTF-8 encoding by default.
• R inconsistency: Character encoding of string
constants.
R-3.0.1/Linux
> Encoding(c('abcxC4xyz', 'abcu00C4xyz'))
[1] "unknown" "UTF-8"
R-3.0.1/Windows (and TERR 2.6)
> Encoding(c('abcxC4xyz', 'abcu00C4xyz'))
[1] "latin1" "UTF-8"
6
R Wrinkles: Character Encoding (2)
• R inconsistency: The R/Windows parser doesn't
accept many UTF-8 chars in names.
R 3.1.0 Linux (and TERR 2.6):
> typeof(parse(text="au30A4")[[1]])
[1] "symbol"
R 3.1.0 Windows:
> typeof(parse(text="au30A4")[[1]])
Error in parse(text = "a<U+30A4>") :
<text>:1:7: unexpected symbol
1: a<U+30A4
^
7
R Wrinkles: Non-Local Returns
• Initially, we had problems using TERR in RStudio,
which executes functions with the following form:
function() {
tryCatch(return(someCalc()),
error=function(e) return("err"))}
• The first argument to tryCatch is evaluated in a
promise, but it can include a “non-local” return that
unwinds the stack to the function where “return”
appears.
8
R Wrinkles: Non-Local Returns (2)
• Consider the following code:
yy <- function(expr, n) {
cat("yy: n=", n, "n")
if (n>1) {
yy(expr, n-1)
} else {
expr
}
"yyret"
}
zz <- function(n) {
yy(return(n), n)
"zzret"
}
• When the expression expr is finally
evaluated in yy, it unwinds multiple
calls to yy, and returns from the
outer function zz:
> zz(3)
yy: n= 3
yy: n= 2
yy: n= 1
[1] 3
>
9
Compatibility Challenge: R’s C API
• Many CRAN packages contain C code calling into the R
engine via C entries (the “Rapi” API).
• We implement Rapi entries in TERR as we find CRAN
packages that need them.
• Problem: the USE_RINTERNALS macro
• If USE_RINTERNALS is defined, some R macros directly access
R object structures, instead of calling Rapi entries.
• This may improve performance for some code
(but this is not a panacea).
• This won't work with TERR, unless we make the TERR object
structure identical to the R object structure.
• Solution: Can gain compatibility for some packages by
rebuilding the package without USE_RINTERNALS
defined.
10
Compatibility Challenge: Packages
Including Base R Code
• Symptom: Trying to run Matrix code, we saw an
unexpected call to .Internal, though it didn’t appear in
the Matrix sources:
> checkMatrix(A)
Error in .Internal:
unimplemented .Internal function: drop
• Reason: S4 setGeneric can incorporate base function
definitions into a generic default method.
• Matrix defines methods for drop, which creates a generic
with base::drop as the default
• Solution: When TERR loads a package with an S4
default method from a system library (base, stats,
graphics, utils), it substitutes the TERR function from
that library.
11
Compatibility Challenge: The
smoothEnds Problem
• Symptom: In the IRanges package, the R system function
stats::smoothEnds works with an IRanges Rle object:
> x <- Rle((-4:4)^2)
> options(dropRle = TRUE)
> stats::smoothEnds(x)
numeric-Rle of length 9 with 9 runs
Lengths: 1 1 1 1 1 1 1 1 1
Values : 16 9 4 1 0 1 4 9 16
• However, this doesn't work in TERR:
> x <- Rle((-4:4)^2)
> options(dropRle = TRUE)
> stats::smoothEnds(x)
Error: 'y' must be numeric
12
Compatibility Challenge: The
smoothEnds Problem (2)
• Reason:
• TERR implements stats::smoothEnds in native C code.
• Most likely, R implements it as R-language code, calling
other arithmetic operators with methods defined for Rle
objects.
• Solution:
• Rewrite TERR’s version of stats::smoothEnds as R-
language code.
• Deeper issue:
• A TERR algorithm has to call the same methods as R, if
they can be redefined for particular object classes.
13
For More Information
• Drop by the TIBCO booth.
• Attend our talks:
• Louis Bajuk-Yorgan: “Deploying R into Business Intelligence and
Real-time Applications”
• (Business track, Session 5, Wednesday 16:00)
• Stephen Kaluzny: “Software Testing and the R Language”
• (Business track, Session 6, Thursday 10:00)
• Spotfire and TERR:
• http://spotfire.tibco.com/terr
• TERR Developer’s Edition:
• http://www.tibcommunity.com/community/products/analytics/terr
• http://tap.tibco.com/
• https://docs.tibco.com/products/tibco-enterprise-runtime-for-r
14

More Related Content

PDF
Максим Харченко. Erlang lincx
PPTX
0.5mln packets per second with Erlang
PDF
Supporting the "Rapi" C-laguage API in an R-compatible engine
PPTX
Java 8 streams
PPTX
Return Oriented Programming (ROP chaining)
PDF
SignalFx: Making Cassandra Perform as a Time Series Database
PDF
Intro to functional programming
PDF
Intro to functional programming
Максим Харченко. Erlang lincx
0.5mln packets per second with Erlang
Supporting the "Rapi" C-laguage API in an R-compatible engine
Java 8 streams
Return Oriented Programming (ROP chaining)
SignalFx: Making Cassandra Perform as a Time Series Database
Intro to functional programming
Intro to functional programming

What's hot (20)

PDF
0.5mln packets per second with Erlang
PPT
Introduction To Functional Programming
PDF
Instrumenting Go (Gopherconindia Lightning talk by Bhasker Kode)
PDF
CNIT 127 Ch 1: Before you Begin
PPTX
PDF
Martin Odersky - Evolution of Scala
PPTX
Java 8 stream and c# 3.5
PPTX
Building APIs with Kotlin and Spark
PDF
CNIT 127: Ch 2: Stack overflows on Linux
PDF
CNIT 127: Ch 3: Shellcode
PDF
Migrating Apache Spark ML Jobs to Spark + Tensorflow on Kubeflow
PDF
0.5mln packets per second with Erlang
PDF
Linked to ArrayList: the full story
PDF
Return Oriented Programming
PDF
Unit testing of spark applications
PPTX
Intro to Functional Programming
PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
CNIT 127: Ch 4: Introduction to format string bugs
PDF
Functional programming in Scala
PPT
06 uml-component
0.5mln packets per second with Erlang
Introduction To Functional Programming
Instrumenting Go (Gopherconindia Lightning talk by Bhasker Kode)
CNIT 127 Ch 1: Before you Begin
Martin Odersky - Evolution of Scala
Java 8 stream and c# 3.5
Building APIs with Kotlin and Spark
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 3: Shellcode
Migrating Apache Spark ML Jobs to Spark + Tensorflow on Kubeflow
0.5mln packets per second with Erlang
Linked to ArrayList: the full story
Return Oriented Programming
Unit testing of spark applications
Intro to Functional Programming
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127: Ch 4: Introduction to format string bugs
Functional programming in Scala
06 uml-component
Ad

Similar to The Compatibility Challenge:Examining R and Developing TERR (20)

PDF
Software Testing and the R language
PPTX
R_L1-Aug-2022.pptx
PDF
EARL Sept 2016 R consortium
PDF
Introduction to Oracle R for Big Data Analysis
PPTX
R reproducibility
PDF
R Consortium update for EARL Boston Oct 2017
PDF
Extend the Reach of R to the Enterprise (for useR! 2013)
PPTX
A Step Towards Reproducibility in R
PDF
a_very_brief_introduction_to_r.pdfhshkdjdn
PDF
Introduction to R for Big Data Analysis
PPTX
DATA MINING USING R (1).pptx
PDF
r merged.pdf
PDF
R consortium update EARL London Sept 2017
PDF
R meet up slides.pptx
PPTX
Big data analytics using R
PDF
R journal 2011-2
PPTX
R Consortium Update for EARL June 2017
PDF
R - the language
PDF
Data analysis in R
PDF
Introduction to R
Software Testing and the R language
R_L1-Aug-2022.pptx
EARL Sept 2016 R consortium
Introduction to Oracle R for Big Data Analysis
R reproducibility
R Consortium update for EARL Boston Oct 2017
Extend the Reach of R to the Enterprise (for useR! 2013)
A Step Towards Reproducibility in R
a_very_brief_introduction_to_r.pdfhshkdjdn
Introduction to R for Big Data Analysis
DATA MINING USING R (1).pptx
r merged.pdf
R consortium update EARL London Sept 2017
R meet up slides.pptx
Big data analytics using R
R journal 2011-2
R Consortium Update for EARL June 2017
R - the language
Data analysis in R
Introduction to R
Ad

More from Lou Bajuk (16)

PDF
Reusing and Managing R models in an Enterprise
PDF
Making Data Science accessible to a wider audience
PDF
Streaming analytics overview for R
PDF
Tibco streaming analytics overview and roadmap
PDF
Embracing data science for smarter analytics apps
PPTX
R in BI and Streaming Applications for useR 2016
PPTX
Applying the R Language to BI and Real Time Applications
PDF
Applying R in BI and Real Time applications EARL London 2015
PPTX
Extending the R language to BI and Real-time Applications JSM 2015
PDF
Using the R Language in BI and Real Time Applications (useR 2015)
PDF
Real time applications using the R Language
PPTX
The Importance of an Analytics Platform
PDF
TERR in BI and Real Time applications
PDF
Deploying R in BI and Real time Applications
PDF
Extending the Reach of R to the Enterprise with TERR and Spotfire
PDF
Sannella use r2013-terr-memory-management
Reusing and Managing R models in an Enterprise
Making Data Science accessible to a wider audience
Streaming analytics overview for R
Tibco streaming analytics overview and roadmap
Embracing data science for smarter analytics apps
R in BI and Streaming Applications for useR 2016
Applying the R Language to BI and Real Time Applications
Applying R in BI and Real Time applications EARL London 2015
Extending the R language to BI and Real-time Applications JSM 2015
Using the R Language in BI and Real Time Applications (useR 2015)
Real time applications using the R Language
The Importance of an Analytics Platform
TERR in BI and Real Time applications
Deploying R in BI and Real time Applications
Extending the Reach of R to the Enterprise with TERR and Spotfire
Sannella use r2013-terr-memory-management

Recently uploaded (20)

PPTX
Camp-Meetings by Pastor Simbaya Bright-WPS Office.pptx
PDF
Printable Lao Gospel Tract - Be Sure of Heaven.pdf
PPT
The Altar Call Training for All Belivers
PPTX
Faith and Gratitude: Guide to the Baccalaureate Mass & Responses
PDF
Printable Malagasy Gospel Tract - Be Sure of Heaven.pdf
PPTX
Tell it to the World. The things that will amaze them more.
PDF
City in the Beyond Nosso Lar Text and Images Chico Xavier Heigorina Cunh...
PPTX
The Human Person as an Embodied Spirit.pptx
PPTX
Lesson study with details and Photos. Easy
PDF
Printable Kurdish Central Sorani Gospel Tract - Be Sure of Heaven.pdf
PDF
Heavenly Holy Spirit vs False Spirit: An Analysis of 1 Peter 1:12 by Matthews...
PPTX
The conversion of Saul to Paul according to the Bible
PDF
Printable Luxembourgish Gospel Tract - Be Sure of Heaven.pdf
PDF
Printable Latvian Gospel Tract - Be Sure of Heaven.pdf
PPTX
Archbishop Louis Mathias - Missionaory.pptx
PPT
Grace of God, kids devotional djfnjdnmxm, ZC,SD v jsdkncjxmc xzcadzgvavc
PDF
Chandogya_Upanishad_by_Swami_Swahananda.pdf
PDF
holistic health - yogic life style for hatha yoga practitioner
PDF
Explaining Sahih Muslim Book 6 – Hadith 216-241
PPTX
Biography of frederick wheeler and John Andrews.pptx
Camp-Meetings by Pastor Simbaya Bright-WPS Office.pptx
Printable Lao Gospel Tract - Be Sure of Heaven.pdf
The Altar Call Training for All Belivers
Faith and Gratitude: Guide to the Baccalaureate Mass & Responses
Printable Malagasy Gospel Tract - Be Sure of Heaven.pdf
Tell it to the World. The things that will amaze them more.
City in the Beyond Nosso Lar Text and Images Chico Xavier Heigorina Cunh...
The Human Person as an Embodied Spirit.pptx
Lesson study with details and Photos. Easy
Printable Kurdish Central Sorani Gospel Tract - Be Sure of Heaven.pdf
Heavenly Holy Spirit vs False Spirit: An Analysis of 1 Peter 1:12 by Matthews...
The conversion of Saul to Paul according to the Bible
Printable Luxembourgish Gospel Tract - Be Sure of Heaven.pdf
Printable Latvian Gospel Tract - Be Sure of Heaven.pdf
Archbishop Louis Mathias - Missionaory.pptx
Grace of God, kids devotional djfnjdnmxm, ZC,SD v jsdkncjxmc xzcadzgvavc
Chandogya_Upanishad_by_Swami_Swahananda.pdf
holistic health - yogic life style for hatha yoga practitioner
Explaining Sahih Muslim Book 6 – Hadith 216-241
Biography of frederick wheeler and John Andrews.pptx

The Compatibility Challenge:Examining R and Developing TERR

  • 1. The Compatibility Challenge: Examining R and Developing TERR Michael Sannella (msannell@tibco.com) TIBCO Spotfire http://spotfire.tibco.com These slides contain all the content from the useR!2014 poster with the same name
  • 2. What is TERR? • TERR: TIBCO® Enterprise Runtime for R • A commercial R-compatible statistics engine. • The latest in a family of scripting engines. • S, S-PLUS®, R, TERR • Embedded in Spotfire® • Provides R scripting, powers analytic tools. • Free Developer's Edition available. • Commercially available for custom integration. 2
  • 3. Why R Compatibility? • We developed TERR as a completely independent engine, without looking at R sources or documentation. • However, we want TERR to be able to load and run R packages from CRAN and other repositories, so TERR must be compatible with R. • Many CRAN packages work with TERR without change. • Some CRAN packages can be made to work with TERR if we rebuild them, without changing any of their code. • Some (hopefully few) CRAN packages require code changes to make them work under TERR. 3
  • 4. How to Achieve R Compatibility? • Our approach: Examine R’s behavior, write unit tests, and make TERR pass these tests. • R compatibility is a moving target: As R and the CRAN packages change over time, we must update TERR to follow. • R Wrinkles: While tracking down TERR incompatibility issues, we uncovered some interesting R behavior. • In some cases, we decided not to make TERR completely compatible with R. 4
  • 5. Developing Compatibility Using Key Packages • For some elements of R, a good way to develop compatibility was to focus on a key package or application that exercises certain functionality. • Key package: The CRAN Matrix package • Matrix fully exercised the S4 object system. • Getting the Matrix tests working gave us confidence that we had a compatible implementation of S4. • Key application: The RStudio GUI • RStudio exercised the R engine entries used to embed R within an application. • RStudio exercised source-code access via the “srcref” attribute on parsed expressions and functions. • RStudio exercised the trace, browser functions used for breakpoints and single-stepping. 5
  • 6. R Wrinkles: Character Encoding • TERR/R Incompatibility: R uses the "unknown" character encoding for most strings, whereas TERR uses the UTF-8 encoding by default. • R inconsistency: Character encoding of string constants. R-3.0.1/Linux > Encoding(c('abcxC4xyz', 'abcu00C4xyz')) [1] "unknown" "UTF-8" R-3.0.1/Windows (and TERR 2.6) > Encoding(c('abcxC4xyz', 'abcu00C4xyz')) [1] "latin1" "UTF-8" 6
  • 7. R Wrinkles: Character Encoding (2) • R inconsistency: The R/Windows parser doesn't accept many UTF-8 chars in names. R 3.1.0 Linux (and TERR 2.6): > typeof(parse(text="au30A4")[[1]]) [1] "symbol" R 3.1.0 Windows: > typeof(parse(text="au30A4")[[1]]) Error in parse(text = "a<U+30A4>") : <text>:1:7: unexpected symbol 1: a<U+30A4 ^ 7
  • 8. R Wrinkles: Non-Local Returns • Initially, we had problems using TERR in RStudio, which executes functions with the following form: function() { tryCatch(return(someCalc()), error=function(e) return("err"))} • The first argument to tryCatch is evaluated in a promise, but it can include a “non-local” return that unwinds the stack to the function where “return” appears. 8
  • 9. R Wrinkles: Non-Local Returns (2) • Consider the following code: yy <- function(expr, n) { cat("yy: n=", n, "n") if (n>1) { yy(expr, n-1) } else { expr } "yyret" } zz <- function(n) { yy(return(n), n) "zzret" } • When the expression expr is finally evaluated in yy, it unwinds multiple calls to yy, and returns from the outer function zz: > zz(3) yy: n= 3 yy: n= 2 yy: n= 1 [1] 3 > 9
  • 10. Compatibility Challenge: R’s C API • Many CRAN packages contain C code calling into the R engine via C entries (the “Rapi” API). • We implement Rapi entries in TERR as we find CRAN packages that need them. • Problem: the USE_RINTERNALS macro • If USE_RINTERNALS is defined, some R macros directly access R object structures, instead of calling Rapi entries. • This may improve performance for some code (but this is not a panacea). • This won't work with TERR, unless we make the TERR object structure identical to the R object structure. • Solution: Can gain compatibility for some packages by rebuilding the package without USE_RINTERNALS defined. 10
  • 11. Compatibility Challenge: Packages Including Base R Code • Symptom: Trying to run Matrix code, we saw an unexpected call to .Internal, though it didn’t appear in the Matrix sources: > checkMatrix(A) Error in .Internal: unimplemented .Internal function: drop • Reason: S4 setGeneric can incorporate base function definitions into a generic default method. • Matrix defines methods for drop, which creates a generic with base::drop as the default • Solution: When TERR loads a package with an S4 default method from a system library (base, stats, graphics, utils), it substitutes the TERR function from that library. 11
  • 12. Compatibility Challenge: The smoothEnds Problem • Symptom: In the IRanges package, the R system function stats::smoothEnds works with an IRanges Rle object: > x <- Rle((-4:4)^2) > options(dropRle = TRUE) > stats::smoothEnds(x) numeric-Rle of length 9 with 9 runs Lengths: 1 1 1 1 1 1 1 1 1 Values : 16 9 4 1 0 1 4 9 16 • However, this doesn't work in TERR: > x <- Rle((-4:4)^2) > options(dropRle = TRUE) > stats::smoothEnds(x) Error: 'y' must be numeric 12
  • 13. Compatibility Challenge: The smoothEnds Problem (2) • Reason: • TERR implements stats::smoothEnds in native C code. • Most likely, R implements it as R-language code, calling other arithmetic operators with methods defined for Rle objects. • Solution: • Rewrite TERR’s version of stats::smoothEnds as R- language code. • Deeper issue: • A TERR algorithm has to call the same methods as R, if they can be redefined for particular object classes. 13
  • 14. For More Information • Drop by the TIBCO booth. • Attend our talks: • Louis Bajuk-Yorgan: “Deploying R into Business Intelligence and Real-time Applications” • (Business track, Session 5, Wednesday 16:00) • Stephen Kaluzny: “Software Testing and the R Language” • (Business track, Session 6, Thursday 10:00) • Spotfire and TERR: • http://spotfire.tibco.com/terr • TERR Developer’s Edition: • http://www.tibcommunity.com/community/products/analytics/terr • http://tap.tibco.com/ • https://docs.tibco.com/products/tibco-enterprise-runtime-for-r 14