Interface with froth from R
RInterface.Rd
Methods to communicate with the froth environment without dropping into a REPL.
Arguments
- object
An R object to push to the froth stack
- nobj
Number of objects to pop from the froth stack
- name
Froth name for
fun
; see Examples- fun
An R function to define within froth
- nargs
Number of arguments expected for
fun
Details
These functions allow interaction with the froth stack from R. froth.RPush
and froth.RPop
allow push/pop operations on the froth stack. These operations are called from R, so pushing any R object is supported.
Some functions are easier to define using R than froth. froth.RDefine
creates a froth function wrapper to call a specified R function, and then builds it into the froth environment. This makes using functions like rnorm
within froth easier; see below for an illustrative example.
Functions defined with froth.RDefine
expect their arguments to be popped directly off the froth stack, with the top of the stack corresponding to the last argument of the function.
Value
froth.RPop
returns a list with the top nobj
elements of the stack.
froth.RPush
and froth.RDefine
invisibly return an integer corresponding to the status of the operation. 0 indicates normal completion.
Author
Aidan Lakshman ahl27@pitt.edu
Note
Functions defined with froth.RDefine
will not be saved using saveFrothSession
.
Examples
## Example of calling rnorm in froth
## rnorm expects 3 arguments: rnorm(n, mean, sd)
froth.RDefine(name='R_rnorm', fun=rnorm, nargs=3L)
## Now we can call rnorm from froth using the 'R_rnorm' word.
## Note that the arguments are expected on the stack
## such that the top of the stack is `sd`,
## the second is `mean`, and the third is `n`.
## n
froth.RPush(5)
## mean
froth.RPush(0.0)
## sd
froth.RPush(1.0)
## running the function
## note this will push the results back onto the stack
froth.parse("R_rnorm")
## we can get the result with froth.RPop
froth.RPop(5L)
#> [[1]]
#> [1] -1.400044
#>
#> [[2]]
#> [1] 0.2553171
#>
#> [[3]]
#> [1] -2.437264
#>
#> [[4]]
#> [1] -0.005571287
#>
#> [[5]]
#> [1] 0.6215527
#>
## As a oneliner: (doesn't return the values)
froth.parse("5 0 1 R_rnorm .s")
#> [[1]]
#> [1] 1.148412
#>
#> [[2]]
#> [1] -1.821818
#>
#> [[3]]
#> [1] -0.2473253
#>
#> [[4]]
#> [1] -0.2441996
#>
#> [[5]]
#> [1] -0.2827054
#>
#> [[6]]
#> NULL
#>