REvaluate
REvaluate[code]
evaluates a string of R code, and returns the result as a Wolfram Language expression.
Details and Options
- 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 REvaluate.
- These are the R types explicitly supported by RLink, elements of which can be returned by REvaluate, 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 REvaluate evaluates to a type not handled by REvaluate, REvaluate returns $Failed.
Examples
open allclose allBasic Examples (12)Summary of the most common use cases

https://wolfram.com/xid/0ptseqibvvfwq8-h8fcah
This constructs a list (R vector) of 10 consecutive integers:

https://wolfram.com/xid/0ptseqibvvfwq8-bpfv27

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

https://wolfram.com/xid/0ptseqibvvfwq8-e2ka7m

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

https://wolfram.com/xid/0ptseqibvvfwq8-iesvg8
When scalars are entered, they are automatically considered vectors of length 1 by R. This is reflected in RLink:

https://wolfram.com/xid/0ptseqibvvfwq8-cwi4x7

By default, real numeric quantities are interpreted as doubles:

https://wolfram.com/xid/0ptseqibvvfwq8-cfoa29

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

https://wolfram.com/xid/0ptseqibvvfwq8-c688tp

This returns a more general sequence:

https://wolfram.com/xid/0ptseqibvvfwq8-fr6qf2

The Wolfram Language's equivalent follows:

https://wolfram.com/xid/0ptseqibvvfwq8-hworz9

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

https://wolfram.com/xid/0ptseqibvvfwq8-ez126m

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

https://wolfram.com/xid/0ptseqibvvfwq8-ehidlp

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

https://wolfram.com/xid/0ptseqibvvfwq8-f27tzk



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:

https://wolfram.com/xid/0ptseqibvvfwq8-d8bgcc

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

https://wolfram.com/xid/0ptseqibvvfwq8-dbryvu

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

https://wolfram.com/xid/0ptseqibvvfwq8-ec8zj8

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

https://wolfram.com/xid/0ptseqibvvfwq8-diux3p

Note that you have to escape the quotation marks.
This generates a random 5×5 matrix:

https://wolfram.com/xid/0ptseqibvvfwq8-c6qd4u

This computes eigenvalues and eigenvectors:

https://wolfram.com/xid/0ptseqibvvfwq8-b98aac

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:

https://wolfram.com/xid/0ptseqibvvfwq8-j34l5b


https://wolfram.com/xid/0ptseqibvvfwq8-dcenle

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

https://wolfram.com/xid/0ptseqibvvfwq8-eow0cq

This creates a random matrix in the Wolfram Language:

https://wolfram.com/xid/0ptseqibvvfwq8-g00o0z


https://wolfram.com/xid/0ptseqibvvfwq8-k3mubb

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

https://wolfram.com/xid/0ptseqibvvfwq8-bad11i

Test that the result is of a logical type:

https://wolfram.com/xid/0ptseqibvvfwq8-ga1yrh

This lists the current objects present in your R workspace:

https://wolfram.com/xid/0ptseqibvvfwq8-g0io4d

Scope (8)Survey of the scope of standard use cases
Assignments (3)

https://wolfram.com/xid/0ptseqibvvfwq8-pwbj4z
You can also use REvaluate to make assignments. So you can, for example, do the following assignment to a global variable myint in the R workspace:

https://wolfram.com/xid/0ptseqibvvfwq8-birx2d

REvaluate can now be used to check the content of the newly created global variable myint:

https://wolfram.com/xid/0ptseqibvvfwq8-epoetm

The following assignment will return a function reference:

https://wolfram.com/xid/0ptseqibvvfwq8-bju2ue

It may be used either on the R side (since this function was given a name in R):

https://wolfram.com/xid/0ptseqibvvfwq8-bl1t11

Or on the Wolfram Language side:

https://wolfram.com/xid/0ptseqibvvfwq8-5gadh

To make assignments but suppress returning the result back to the Wolfram Language, you can suppress the output by adding a semicolon at the end:

https://wolfram.com/xid/0ptseqibvvfwq8-krcu4u

https://wolfram.com/xid/0ptseqibvvfwq8-hllfdm

Multidimensional Arrays (3)

https://wolfram.com/xid/0ptseqibvvfwq8-fbrmw
While you can handle multidimensional arrays with REvaluate just as easily as one-dimensional arrays, there are a few subtleties.
This will create a random one-dimensional array in R:

https://wolfram.com/xid/0ptseqibvvfwq8-dqlp70

Since R stores arrays in column-major order, the previous result is not what you might have expected. But this is needed for consistency regarding array indexing:

https://wolfram.com/xid/0ptseqibvvfwq8-qyecw


https://wolfram.com/xid/0ptseqibvvfwq8-b7bakd


https://wolfram.com/xid/0ptseqibvvfwq8-ile3tx


https://wolfram.com/xid/0ptseqibvvfwq8-d8ayvb

To convert the array to row-major order (which is the order used by the Wolfram Language), you can use Transpose:

https://wolfram.com/xid/0ptseqibvvfwq8-jy26cp

https://wolfram.com/xid/0ptseqibvvfwq8-jbgw22


https://wolfram.com/xid/0ptseqibvvfwq8-db7v87

And the same for indexing for the converted array:

https://wolfram.com/xid/0ptseqibvvfwq8-tcj6h


https://wolfram.com/xid/0ptseqibvvfwq8-cyz0u8

The above issues only show up for arrays created in R and then sent to the Wolfram Language. If you create an array in the Wolfram Language and then send it to R, it is automatically transformed to column-major order, and back to row-major order when you get it back from R. Therefore, in such cases, you get back the original array.
This creates an array in the Wolfram Language:

https://wolfram.com/xid/0ptseqibvvfwq8-emx29y


https://wolfram.com/xid/0ptseqibvvfwq8-e11vzk

https://wolfram.com/xid/0ptseqibvvfwq8-gl68jc

As before, the indexing is consistent with that in the Wolfram Language:

https://wolfram.com/xid/0ptseqibvvfwq8-bbgcpr


https://wolfram.com/xid/0ptseqibvvfwq8-eqjdzj

This means that you can send Wolfram Language arrays to R and continue working with them in R, just like you would in the Wolfram Language.
As another example, consider matrix multiplication. First, construct a matrix in the Wolfram Language:

https://wolfram.com/xid/0ptseqibvvfwq8-irr1i2

Now send it to R, assigning it to a variable mmat in the R workspace:

https://wolfram.com/xid/0ptseqibvvfwq8-moy9np

This will perform the matrix multiplication:

https://wolfram.com/xid/0ptseqibvvfwq8-fcnzen

It yields the same result as it would in the Wolfram Language:

https://wolfram.com/xid/0ptseqibvvfwq8-bo65d7

Special Quantities: NaNs and Infinities (1)

https://wolfram.com/xid/0ptseqibvvfwq8-cq6n6e
There is currently a one-directional support for special quantities such as NaNs and infinities of various kinds.
The following R input demonstrates how various quantities are translated into the Wolfram Language. This is what you would get directly in the R console:

https://wolfram.com/xid/0ptseqibvvfwq8-nx1pf


https://wolfram.com/xid/0ptseqibvvfwq8-dax8sk

In order to get a complex result when taking a square root of a negative number, that number must be entered manifestly as a complex number:

https://wolfram.com/xid/0ptseqibvvfwq8-bdjgm7

While getting such quantities from R to the Wolfram Language is supported, the reverse is currently not:

https://wolfram.com/xid/0ptseqibvvfwq8-cse1a


Missing Points (1)

https://wolfram.com/xid/0ptseqibvvfwq8-gs5xx6
Vectors and lists in R support missing points. In the Wolfram Language, such points are represented by Missing[].
Here is a typical example for a vector:

https://wolfram.com/xid/0ptseqibvvfwq8-c5mu02


https://wolfram.com/xid/0ptseqibvvfwq8-nlaj5c

The action of most functions on missing elements is to produce another missing element. For example:

https://wolfram.com/xid/0ptseqibvvfwq8-mfv28d

Missing points have a bidirectional support in RLink. For example, the following performs the same computation as the previous example, but with the data sent to R from the Wolfram Language:

https://wolfram.com/xid/0ptseqibvvfwq8-ho0zdx

For a number of reasons described in the tutorial "Functions" on R functions, the following syntax is much preferred:

https://wolfram.com/xid/0ptseqibvvfwq8-jgnsn9

Generalizations & Extensions (2)Generalized and extended use cases
Defining Your Own Functions (1)

https://wolfram.com/xid/0ptseqibvvfwq8-g1i4kx
Here is an example of one possible implementation of the Wolfram Language Tuples function in R, returning tuples as data frames:

https://wolfram.com/xid/0ptseqibvvfwq8-cbnw52

https://wolfram.com/xid/0ptseqibvvfwq8-e5f3k7

Manipulating R Objects' Attributes on the R Side, and General R Objects (1)

https://wolfram.com/xid/0ptseqibvvfwq8-boecld
All objects in R can have one or several attributes. In RLink, R objects with generic attributes are represented on the Wolfram side by Wolfram Language expressions with the head RObject. Some aspects of this also affect REvaluate.
The following will construct a matrix in the Wolfram Language, send it to R, and assign it to a variable myInt in the R workspace:

https://wolfram.com/xid/0ptseqibvvfwq8-b7zuzj

You can inspect the class of the resulting R object stored in a variable:

https://wolfram.com/xid/0ptseqibvvfwq8-baehn2

Attributes of any R object are stored in an R list. Objects of the class matrix have at least one attribute, "dim", which stores the information about dimensions (dimensions themselves are represented as an integer vector):

https://wolfram.com/xid/0ptseqibvvfwq8-d6xitv

You can see that this list itself has attributes, in particular the attribute "names", and therefore it cannot be represented as just a Wolfram Language list, but has a head RObject. One shortcut to retrieve the value of a given attribute is to use the R attr function:

https://wolfram.com/xid/0ptseqibvvfwq8-fq8zzv

You can use REvaluate to perform non-trivial reshaping of your matrix on the R side by modifying its dim attribute. For example, you may convert the matrix into a three-dimensional array with dimensions {2,5,2}:

https://wolfram.com/xid/0ptseqibvvfwq8-b1bu0x

Which results in the following:

https://wolfram.com/xid/0ptseqibvvfwq8-hjbs55

At some point, you may wish to add some new attributes to myInt, for example:

https://wolfram.com/xid/0ptseqibvvfwq8-fj3ikt


https://wolfram.com/xid/0ptseqibvvfwq8-7mlhq

The dim attribute is implicit. You can always use RObject-based general representation, even for objects with no attributes:

https://wolfram.com/xid/0ptseqibvvfwq8-fd6gzx

Wolfram Research (2012), REvaluate, Wolfram Language function, https://reference.wolfram.com/language/RLink/ref/REvaluate.html.
Text
Wolfram Research (2012), REvaluate, Wolfram Language function, https://reference.wolfram.com/language/RLink/ref/REvaluate.html.
Wolfram Research (2012), REvaluate, Wolfram Language function, https://reference.wolfram.com/language/RLink/ref/REvaluate.html.
CMS
Wolfram Language. 2012. "REvaluate." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/RLink/ref/REvaluate.html.
Wolfram Language. 2012. "REvaluate." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/RLink/ref/REvaluate.html.
APA
Wolfram Language. (2012). REvaluate. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/RLink/ref/REvaluate.html
Wolfram Language. (2012). REvaluate. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/RLink/ref/REvaluate.html
BibTeX
@misc{reference.wolfram_2024_revaluate, author="Wolfram Research", title="{REvaluate}", year="2012", howpublished="\url{https://reference.wolfram.com/language/RLink/ref/REvaluate.html}", note=[Accessed: 07-June-2025
]}
BibLaTeX
@online{reference.wolfram_2024_revaluate, organization={Wolfram Research}, title={REvaluate}, year={2012}, url={https://reference.wolfram.com/language/RLink/ref/REvaluate.html}, note=[Accessed: 07-June-2025
]}