RLink`
RLink`

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.

更多信息和选项

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

范例

打开所有单元关闭所有单元

基本范例  (8)

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

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

Now call this function:

You can now plot the result:

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

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:

Now it can be used:

You can also use it directly:

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:

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

Here is a test message:

The message will now be split:

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

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

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:

You can now generate a random sample in R:

And test:

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

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

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

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

Neat Examples  (1)

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:

Now, generate closure for a particular vector:

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:

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

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

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

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:

Wolfram Research (2012),RFunction,Wolfram 语言函数,https://reference.wolfram.com/language/RLink/ref/RFunction.html.

文本

Wolfram Research (2012),RFunction,Wolfram 语言函数,https://reference.wolfram.com/language/RLink/ref/RFunction.html.

CMS

Wolfram 语言. 2012. "RFunction." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/RLink/ref/RFunction.html.

APA

Wolfram 语言. (2012). RFunction. Wolfram 语言与系统参考资料中心. 追溯自 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-November-2024 ]}

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-November-2024 ]}