15
INTERFACING R TO OTHER LANGUAGES
R is a great language, but it can’t do every-
thing well. Thus, it is sometimes desirable
to call code written in other languages from
R. Conversely, when working in other great lan-
guages, you may encounter tasks that could be better
done in R.
R interfaces have been developed for a number of other languages,
from ubiquitous languages like C to esoteric ones like the Yacas computer
algebra system. This chapter will cover two interfaces: one for calling
C/C++ from R and the other for calling R from Python.
15.1 Writing C/C++ Functions to Be Called from R.................................
You may wish to write your own C/C++ functions to be called from R. Typ-
ically, the goal is performance enhancement, since C/C++ code may run
much faster than R, even if you use vectorization and other R optimization
techniques to speed things up.
Another possible goal in dropping down to the C/C++ level is special-
ized I/O. For example, R uses the TCP protocol in layer 3 of the standard
Internet communication system, but UDP can be faster in some settings.