# REvaluate

REvaluate[code]

evaluates a string of R code, and returns the result as a Wolfram Language 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 the Wolfram Language 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 the Wolfram Language 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)

In[1]:= |

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

In[1]:= |

Out[1]= |

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

In[1]:= |

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:** **

In[2]:= |

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

In[1]:= |

Out[1]= |

By default, real numeric quantities are interpreted as doubles:

In[2]:= |

Out[2]= |

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

In[3]:= |

Out[3]= |

This returns a more general sequence:

In[1]:= |

Out[1]= |

The Wolfram Language's equivalent follows:

In[2]:= |

Out[2]= |

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

In[1]:= |

Out[1]= |

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

In[2]:= |

Out[2]= |

The second method is slower, however, just as it would be in the Wolfram Language:

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:

In[1]:= |

Out[1]= |

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

In[2]:= |

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):

In[1]:= |

Out[1]= |

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

In[1]:= |

Out[1]= |

Note that you have to escape the quotation marks.

This generates a random 5×5 matrix:

In[1]:= |

Out[1]= |

This computes eigenvalues and eigenvectors:

In[2]:= |

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:

In[3]:= |

Out[3]= |

In[4]:= |

Out[4]= |

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

In[5]:= |

Out[5]= |

This creates a random matrix in the Wolfram Language:

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

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

In[3]:= |

Out[3]= |

Test that the result is of a logical type:

In[4]:= |

Out[4]= |

This lists the current objects present in your R workspace:

In[1]:= |

Out[1]= |