Chapter 11 R Object - Function

// #[rustr_export]
pub fn call_sort(x:NumVec)->RResult<NumVec>{
    let res = try!(RFun::from_str_global("sort"));
    Ok(
        try!(   // expect a NumVec return 
            res.eval::<NumVec>(&[&x]) // pass varibles 
            ))
}
rust(code = '
// #[rustr_export]
pub fn call_sort(x:NumVec)->RResult<NumVec>{
    let res = try!(RFun::from_str_global("sort"));
    Ok(
        try!(   // expect a NumVec return 
            res.eval::<NumVec>(&[&x]) // pass varibles 
            ))
}
')

call_sort(c(2,1))
#> [1] 1 2

call_sort(c(2,1,3))
#> [1] 1 2 3

call_sort("sd")
#> Error in eval(expr, envir, enclos): NotCompatible: expecting a REALSXP

rand = runif(100000)

microbenchmark(call_sort(rand),sort(rand))
#> Unit: milliseconds
#>             expr  min   lq mean median   uq  max neval
#>  call_sort(rand) 12.0 12.3 14.7   13.2 13.6 67.4   100
#>       sort(rand) 11.8 12.0 13.2   12.3 13.3 63.5   100

The speed of Calling R functions in Rust is slower than the speed of calling them directly in R.

Also read the docs http://docs.rustr.org/rustr/rfunction/struct.RFunM.html .