Introduction to RLink
R is a programming language and software environment for statistical computing and graphics. R is an open-source project, and a result of a large community effort. More information about R can be found at http://www.r-project.org.
RLink is a Wolfram System application that uses JLink and RJava/JRI Java libraries to link to the R functionality. It allows the user to communicate data between the Wolfram Language and R and execute R code from within the Wolfram Language.
This tutorial is intended to give a very quick snapshot of what RLink allows you to do and the typical RLink workflow. For a more in-depth account on RLink, please consult the "RLink User Guide", and the tutorials "Functions" and "R Data Types in RLink", as well as the documentation pages for individual functions of the RLink API, listed in "Reference".
You are now ready to work. The workflow will normally contain iterations of the following three steps: send data from the Wolfram Language to R, execute some R code, and get the result back to the Wolfram Language. In what follows, a brief and rather condensed exposition of the typical RLink workflow will be given.
RSet was used to send the data to R, assigning it to some variable (or expression) in the R workspace, and REvaluate was used to evaluate a string of R code and return the result back to the Wolfram Language. In fact, REvaluate combines the last two of the three steps mentioned, since it both evaluates the code and returns the result.
The result is wrapped in a List. This is because it is in fact a vector with a single logical element, from the viewpoint of R.
To call R functions with a more streamlined syntax, you can use RFunction. You can use it to have a more concise solution for the previous example, and avoid the step introducing a global variable.
This shows some of the differences in the type systems of R and the Wolfram Language. What was returned is a list of lists, each sublist having two elements: a one-element integer vector (original number), and a one-element logical vector (the value of the test predicate).
You can use the ToRForm function to see the internal RLink representation of data, which can be both input data from the Wolfram Language and the results obtained from R. In this particular case, the preceding description of the structure of the result is easy to confirm.
This is the internal form of the expression stored in result, which RLink uses to communicate the data to and from R. You can use this form in RSet, RFunction, and REvaluate, just like a brief form. Most of the time, however, this form is much less convenient to use.
You can also use the FromRForm function to perform an inverse transformation.
As a matter of fact, often the structure of an expression in a short form can be simplified. For example, the inner lists in the result could have been flattened, and RLink would still interpret it correctly.
There are a few cases when the automatic type identification in RLink is ambiguous. One notable case is when you want to construct a list of elements of the same native type: in such a case it will be interpreted as a vector by default. It is possible to force the list interpretation; more details on that can be found on the documentation page for RList, and also in "Data Types".
You are not confined to R one-liners when working with RLink. To illustrate this point, consider an example: a Wolfram Language-style Split function will be implemented in R, which will work on vectors.