REvaluate[code]
evaluates a string of R code, and returns the result as a Wolfram Language expression.
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 all close allBasic Examples (12)
Needs["RLink`"]
InstallR[]This constructs a list (R vector) of 10 consecutive integers:
REvaluate["1:10"]This uses a general vector constructor in R to construct a vector of numbers:
REvaluate["c(c(1,2),c(3,4))"]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:
REvaluate["c(c(1,2),c(3,4));"]When scalars are entered, they are automatically considered vectors of length 1 by R. This is reflected in RLink:
REvaluate["5"]By default, real numeric quantities are interpreted as doubles:
REvaluate["typeof(5)"]One way to have R interpret them as integers is to use R's as.integer function:
REvaluate["as.integer(5)"]This returns a more general sequence:
REvaluate["seq(0,pi,by=0.2)"]The Wolfram Language's equivalent follows:
Range[0, Pi, 0.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):
REvaluate["sin(seq(0,pi,by=0.2))"]This is the same, but using explicit function mapping (sapply in R corresponds to Map in the Wolfram Language):
REvaluate["sapply(seq(0,pi,by=0.2),sin)"]The second method is slower, however, just as it would be in the Wolfram Language:
REvaluate["largeSeq <- seq(0,2*pi,0.0001)"] // Short
REvaluate["sin(largeSeq)"] // Short // AbsoluteTiming
REvaluate["sapply(largeSeq,sin)"] // Short // AbsoluteTimingThis 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:
REvaluate["t(sapply(seq(0,pi,by=0.2),function(nm){c(nm,sin(nm))}))"]Here is the Wolfram Language's equivalent for the rhs of the assignment from the previous example:
Map[{#, Sin[#]} &, Range[0, Pi, 0.2]]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):
REvaluate["
{
x <- c(0,1,1,2,3,5,8,13,21,34)
mx<- mean(x)
medx<-median(x)
sdx <- sd(x)
varx<- var(x)
results <-c(mx,medx,sdx,varx)
}"]This splits a string, and actually returns a list rather than a vector, which is reflected in extra curly braces:
REvaluate["strsplit(\"This is a test message\",NULL)"]Note that you have to escape the quotation marks.
This generates a random 5×5 matrix:
mat = REvaluate[
"rmat <- array(sample(1:15, 16, replace = TRUE, prob = NULL),dim = \
c(4,4))"]This computes eigenvalues and eigenvectors:
REvaluate["esys <- eigen(rmat,sym = FALSE)"]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:
REvaluate["esys$vectors"]REvaluate["esys$values"]This is the same in the Wolfram Language (eigenvectors do not necessarily have to be the same in this case):
Eigensystem[N@mat]This creates a random matrix in the Wolfram Language:
testVector = Partition[RandomInteger[10, 12], 3]RSet["testVector", testVector]The result of this element-wise logical comparison is a logical vector (matrix):
REvaluate["lvec <- testVector>5"]Test that the result is of a logical type:
REvaluate["typeof(lvec)"]This lists the current objects present in your R workspace:
REvaluate["objects()"]Scope (8)
Assignments (3)
Needs["RLink`"]
InstallR[]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:
REvaluate["myint <- 1:10"]REvaluate can now be used to check the content of the newly created global variable myint:
REvaluate["myint"]The following assignment will return a function reference:
myFun = REvaluate["myFun <- function(x){x^2}"]It may be used either on the R side (since this function was given a name in R):
REvaluate["myFun(5)"]Or on the Wolfram Language side:
myFun[5]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:
REvaluate["myFun1 <- function(x){x^4};"]REvaluate["myFun1(3)"]Multidimensional Arrays (3)
Needs["RLink`"]
InstallR[]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:
arr = REvaluate["arr <- array(1:20, dim = c(5,2,2))"]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:
arr[[1]]REvaluate["arr[1,,]"]arr[[All, 1]]REvaluate["arr[,1,]"]To convert the array to row-major order (which is the order used by the Wolfram Language), you can use Transpose:
convertToRowMajor[arr_] :=
Transpose[arr, Reverse@Range@Length@Dimensions@arr]rmarr = convertToRowMajor[arr]Dimensions[rmarr]And the same for indexing for the converted array:
REvaluate["arr[,1,1]"]convertToRowMajor[arr][[1, 1, All]]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:
marr = Map[Partition[#, 2] &, Partition[Range[20], 4]]RSet["marr", marr];REvaluate["marr"]As before, the indexing is consistent with that in the Wolfram Language:
REvaluate["marr[1,,]"]marr[[1]]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:
mmat = Partition[Range[20], 4]Now send it to R, assigning it to a variable mmat in the R workspace:
RSet["mmat", mmat]This will perform the matrix multiplication:
REvaluate["mmat %*% t(mmat)"]It yields the same result as it would in the Wolfram Language:
mmat . Transpose[mmat]Special Quantities: NaNs and Infinities (1)
Needs["RLink`"]
InstallR[]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:
REvaluate[" as.character(c(1,sqrt(-1),1/0,-1/0,(1+1i)/0))"]REvaluate["c(1,sqrt(-1),1/0,-1/0,(1+1i)/0)"]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:
REvaluate["sqrt(-1+0i)"]While getting such quantities from R to the Wolfram Language is supported, the reverse is currently not:
RSet["myTest", REvaluate["as.list(c(1,sqrt(-1),1/0,-1/0,(1+1i)/0))"]]Missing Points (1)
Needs["RLink`"]
InstallR[]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:
mvec = REvaluate["mvec <- c(1,2,NA,3,NA,NA,5)"]REvaluate["list(1,2,NA,3,NA,NA,5)"]The action of most functions on missing elements is to produce another missing element. For example:
REvaluate["mvec^2"]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:
REvaluate["function(x){x^2}"][mvec]For a number of reasons described in the tutorial "Functions" on R functions, the following syntax is much preferred:
RFunction["function(x){x^2}"][mvec]Generalizations & Extensions (2)
Defining Your Own Functions (1)
Needs["RLink`"]
InstallR[]Here is an example of one possible implementation of the Wolfram Language Tuples function in R, returning tuples as data frames:
REvaluate["tuples <- function(x, n) do.call(expand.grid, rep(list(x), \
n));"]REvaluate["tuples(0:1, 3)"]Manipulating R Objects' Attributes on the R Side, and General R Objects (1)
Needs["RLink`"]
InstallR[]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:
RSet["myInt", Partition[Range[20], 5]]You can inspect the class of the resulting R object stored in a variable:
REvaluate["class(myInt)"]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):
REvaluate["attributes(myInt)"]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:
REvaluate["attr(myInt,\"dim\")"]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}:
REvaluate["attr(myInt,\"dim\") <- c(2,5,2)"]Which results in the following:
REvaluate["myInt"]At some point, you may wish to add some new attributes to myInt, for example:
REvaluate["attr(myInt,\"myAttr\") <- \"myAttrValue\""]REvaluate["myInt"]The dim attribute is implicit. You can always use RObject-based general representation, even for objects with no attributes:
RSet["mySecondInt",
RObject[{{{1, 13}, {11, 4}, {2, 14}, {12, 5}, {3, 15}}, {{6,
18}, {16, 9}, {7, 19}, {17, 10}, {8, 20}}}, RAttributes[]]];
REvaluate["mySecondInt"]See Also
Tech Notes
Text
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.
APA
Wolfram Language. (2012). REvaluate. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/RLink/ref/REvaluate.html
BibTeX
@misc{reference.wolfram_2026_revaluate, author="Wolfram Research", title="{REvaluate}", year="2012", howpublished="\url{https://reference.wolfram.com/language/RLink/ref/REvaluate.html}", note=[Accessed: 12-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_revaluate, organization={Wolfram Research}, title={REvaluate}, year={2012}, url={https://reference.wolfram.com/language/RLink/ref/REvaluate.html}, note=[Accessed: 12-June-2026]}