# 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[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 object on arguments args.

## DetailsDetails

- 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.

## ExamplesExamplesopen allclose all

### Basic Examples (8)Basic Examples (8)

You need to load the package and install R runtime before you can work with it:

In[1]:= |

This creates an opaque reference to a built-in function:

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

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

In[4]:= |

Out[4]//FullForm= | |

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:

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

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:

In[1]:= |

Out[1]= |

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

In[1]:= |

Out[1]= |

In[2]:= |

The message will now be split:

In[3]:= |

Out[3]= |

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

In[4]:= |

Out[4]= |

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

In[1]:= |

Out[1]= |

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:

In[1]:= |

Out[1]= |

You can now generate a random sample in R:

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |

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

In[4]:= |

Out[4]= |

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

In[5]:= |

Out[5]= |

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

In[1]:= |

Out[1]= |

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

In[2]:= |

Out[2]= |

In[3]:= |

Out[3]= |