Example
The following example shows how to execute R statements and exchange data with the R workspace.
model "r example" uses "r" declarations CITIES = {"LONDON", "PARIS", "ROME"} ZONES = 1..4 mosarray, backarr, backarrio: array(ZONES, CITIES) of integer backnum: real end-declarations setparam("Rverbose",true) ! Enable showing R error messages ! Reval evaluates arbitrary R statements Reval("t<-Sys.time();now<-format(t, '%H:%M')") ! Rprint also prints the result (via the R print function) Rprint("paste('Hello from R at',now)") ! Assign some Mosel scalars to R vars and show results Rset("a_num", 1.2) Reval("str(a_num)") Rset("a_chr", "word") Reval("str(a_chr)") ! The lvalue can be any R valid lvalue, e.g. the dim attribute Rset("a_vec", 1..6) ! a_vec is an R vector Rset("dim(a_vec)", [2,3]) ! change its dimensions writeln("a_vec") Rprint("a_vec") ! now it is a 2x3 matrix ! Assign a Mosel array to an R variable forall(i in ZONES, c in CITIES) mosarray(i,c):=i*10+getsize(c) Rset("arr", mos_array) ! The R vector keeps index names writeln("arr") Rprint("arr") ! Retrieve R variables writeln("a_num is ", Rgetreal("a_num")) writeln("a_chr is ", Rgetstr("a_chr")) Rgetarr("arr", backarr) writeln("arr is ", backarr) ! Data can also be exchanged via the I/O driver newnumber:=1.3 mosarray(1,"LONDON"):=1 ! Send data to R initializations to "r.rws:" newnumber as "a_num" mosarray as "arr" end-initializations ! Get data back from R initializations from "r.rws:" backnum as "a_num" backarrio as "arr" end-initializations writeln("backnum is ", backnum) writeln("backarrio is ", backarrio) end-model
© 2001-2020 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.