RLINK SYMBOL

# REvaluate

REvaluate[code]
evaluates a string of R code, and returns the result as a Mathematica expression.

## DetailsDetails

• The code code must be a string of valid R code, which, after execution, produces the result of one of the types that can be handled by .
• These are the R types explicitly supported by RLink, elements of which can be returned by , along with the internal RLink heads of expressions representing such elements:
•  "integer" RVector "double" RVector "complex" RVector "logical" RVector "character" RVector "list" RList "NULL" RNull "closure" RFunction "builtin" RFunction "environment" REnvironment
• Apart from explicitly supported types, most other types can be imported from R into Mathematica by converting them into a string (deparsed R code). Such elements are wrapped into an RCode wrapper.
• The type "environment" is only partially supported at present. Namely, it will consider all environment objects explicitly exported from R into Mathematica to represent global environments.
• When the code passed to evaluates to a type not handled by , returns \$Failed.

## ExamplesExamplesopen allclose all

### Basic Examples (12)Basic Examples (12)

This constructs a list (R vector) of 10 consecutive integers:

 Out[1]=

This uses a general vector constructor in R to construct a vector of numbers:

 Out[1]=

To suppress the output, or when no output is needed, put a semicolon at the end of the R code, in which case returns Null:

When scalars are entered, they are automatically considered vectors of length 1 by R. This is reflected in RLink:

 Out[1]=

By default, real numeric quantities are interpreted as doubles:

 Out[2]=

One way to have R interpret them as integers is to use R's as.integer function:

 Out[3]=

This returns a more general sequence:

 Out[1]=

Mathematica's equivalent follows:

 Out[2]=

This computes the sine function on the sequence from the previous example (most functions in R are vectorized, just as in Mathematica):

 Out[1]=

This is the same, but using explicit function mapping (sapply in R corresponds to Map in Mathematica):

 Out[2]=

The second method is slower, however, just as it would be in Mathematica:

 Out[3]=
 Out[3]=
 Out[3]=

This is a slightly more complex example. Here, a function is mapped that takes a value of the independent variable and returns it back together with the value of the function (here, sine) wrapped in a vector:

 Out[1]=

Here is Mathematica's equivalent for the rhs of the assignment from the previous example:

 Out[2]=

You can use larger pieces of R code inside . In such a case, however, you should wrap your statements in a block (curly braces):

 Out[1]=

This splits a string, and actually returns a list rather than a vector, which is reflected in extra curly braces:

 Out[1]=

Note that you have to escape the quotation marks.

This generates a random 5×5 matrix:

 Out[1]=

This computes eigenvalues and eigenvectors:

 Out[2]=

You can see that the result is an R list and has a non-empty attribute set (attribute names); therefore, it is represented as a general R object with a head RObject. This topic is covered in more details in "R Data Types in RLink" and the RObject reference page.

This extracts only eigenvectors:

 Out[3]=

Or eigenvalues:

 Out[4]=

This is the same in Mathematica (eigenvectors do not necessarily have to be the same in this case):

 Out[5]=

This creates a random matrix in Mathematica:

 Out[1]=

Send it to R:

 Out[2]=

The result of this element-wise logical comparison is a logical vector (matrix):

 Out[3]=

Test that the result is of a logical type:

 Out[4]=

This lists the current objects present in your R workspace:

 Out[1]=

## TutorialsTutorials

New to Mathematica? Find your learning path »
Have a question? Ask support »