RFunction
RFunction[code]
uses code to define a function in the R workspace and returns a reference (handle) to an R function defined in the R workspace, which also has the head RFunction.
RFunction[type,RCode[code],refIndex,attributes ]
represents a reference to an R function defined in the R workspace.
RFunction[type,RCode[code],refIndex,attributes ][args]
calls an R function represented by the RFunction object on arguments args.
Details and Options
- If the function has not been defined before in the current RLink session, RFunction[code] constructs a function defined by R code string code in the R workspace, and returns an opaque Wolfram Language reference to this function, having the longer form RFunction[type,RCode[code],refIndex,attributes]. If the code code does not define a valid R function, $Failed is returned.
- The longer form RFunction[type,RCode[code],refIndex,attributes] represents a FullForm of the R function handle, which is an opaque Wolfram Language reference to the function defined in the R workspace. Normally, this form is returned as a result of calling functions such as RFunction[code] or REvaluate[code], rather than constructed manually.
- The type parameter can be "closure" or "builtin". The former usually indicates that the R function in question is itself written in R language, while the latter means that it is one of the core R built-in functions.
- The type parameter in an RCode wrapper represents the code of the function. If the reference was created as a result of calling RFunction[code], then the code in RCode is exactly the code used to define this R function. If the reference was created as a result of calling REvaluate, the code inside RCode is a so-called deparsed (obtained by using the R deparse function) definition of an R function in question.
- The refIndex parameter can be a positive integer or Automatic. The former case corresponds to the references obtained via calls to REvaluate. The latter case corresponds to the references obtained via calls to RFunction[code].
- References with refIndex as an integer are generally only valid through a single RLink session. In contrast, references with refIndex set to Automatic are valid across Wolfram System or RLink sessions.
- The function's attributes attributes have the head RAttributes.
Examples
open allclose allBasic Examples (8)Summary of the most common use cases
You need to load the package and install R runtime before you can work with it:

https://wolfram.com/xid/0b771ubahhfgcy-h8fcah
This creates an opaque reference to a built-in function:

https://wolfram.com/xid/0b771ubahhfgcy-bkxfdy


https://wolfram.com/xid/0b771ubahhfgcy-h0bfdc


https://wolfram.com/xid/0b771ubahhfgcy-eqi54

Here is how the above function reference is internally represented by RLink:

https://wolfram.com/xid/0b771ubahhfgcy-g73ccw

This expression represents an opaque reference to the R function ("sin" in this case), defined in the R workspace.
This returns a reference to an anonymous function:

https://wolfram.com/xid/0b771ubahhfgcy-jeh8ho


https://wolfram.com/xid/0b771ubahhfgcy-rdr7r


https://wolfram.com/xid/0b771ubahhfgcy-duhlc1

This constructs a reference to a function with zero arguments, which generates a single random number uniformly distributed between 0 and 1, which is immediately called:

https://wolfram.com/xid/0b771ubahhfgcy-xg2eg

Here is a function call to split a string into characters:

https://wolfram.com/xid/0b771ubahhfgcy-cobe6j


https://wolfram.com/xid/0b771ubahhfgcy-h4g59
The message will now be split:

https://wolfram.com/xid/0b771ubahhfgcy-g9alno

Because many R functions are vectorized, you can also use it on a list of strings:

https://wolfram.com/xid/0b771ubahhfgcy-cwye7o

This will apply a function to a multidimension array with some extra attribute(s):

https://wolfram.com/xid/0b771ubahhfgcy-k748e

The following code defines a function that returns positions of the first entries of 1 in runs of ones of length no less than a given length:

https://wolfram.com/xid/0b771ubahhfgcy-i6yx9k

You can now generate a random sample in R:

https://wolfram.com/xid/0b771ubahhfgcy-db93u


https://wolfram.com/xid/0b771ubahhfgcy-n22p6g

Or, you can use the data from the Wolfram Language:

https://wolfram.com/xid/0b771ubahhfgcy-xxm9b

Note that you can also use the function definition directly, without assigning it to some variable:

https://wolfram.com/xid/0b771ubahhfgcy-dqfc8f

This defines a higher-order function that takes a vector and another test function, and selects from the vector elements satisfying the criteria of test function (analog of the Wolfram Language's Select):

https://wolfram.com/xid/0b771ubahhfgcy-dno7rz

This can now be used. For example, select positive numbers from the list (vector):

https://wolfram.com/xid/0b771ubahhfgcy-m41ts1


https://wolfram.com/xid/0b771ubahhfgcy-cbr4b1

Neat Examples (1)Surprising or curious use cases

https://wolfram.com/xid/0b771ubahhfgcy-cfm75x
The following code will create a generator for closures that would perform some custom Select-like operations on a list that will be hard-coded into them. It will return a function that will embed some vector and will expect some test criteria to filter this vector. First, create a generator of closures:

https://wolfram.com/xid/0b771ubahhfgcy-n5gdhl

Now, generate closure for a particular vector:

https://wolfram.com/xid/0b771ubahhfgcy-bb0bo3

You can now use it. Note that this closure is at the same time a higher-order function, since it accepts another (test) function as a parameter. To use it, first define a testing function:

https://wolfram.com/xid/0b771ubahhfgcy-gcveld

This function can be, of course, tested by itself on the Wolfram Language side:

https://wolfram.com/xid/0b771ubahhfgcy-jj6z2a

But now you can use it as a parameter to the closure:

https://wolfram.com/xid/0b771ubahhfgcy-llam6i

You can also use the direct form without storing a function in a variable:

https://wolfram.com/xid/0b771ubahhfgcy-0k4o5

You can also use this form, although by doing so, you will create an extra copy of the test function reference for every such invocation:

https://wolfram.com/xid/0b771ubahhfgcy-9nx04

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