R LANGUAGE COMPONENT
r2369 introduces a component that embeds the R Language in apsim simulations: users are presented with UI tab similar to the existing manager tabs; though the language is R.
R program fragments are associated with apsim events in a familiar fashion – via the tab name.
Users should install a recent (2.14+) version of R, and install two R packages: Rcpp and RInside.
The APSIM component will find the latest installed version of R from the system registry, load it, and call it when the nominated events occur. Unlike other apsim modules, the R interpreter is “shared” between apsim modules – there is only one R namespace.
A sample simulation that demonstrates basic functionality is in the “Examples” directory.
The module can display R Graphics via the usual windows graphics device, but does not support interactive (console) evaluation of R expressions. Debugging R code is limited to print statements (that are recorded in the summary file), and calls to save.image().
The APSIM component implements a R language module “apsim”, that implements 7 functions to interact with a simulation:
value <- apsimGet(name)
Returns the value of an apsim variable called “name”.
Set an apsim variable called “name” to “value”. Value may be any simple (numeric, string, vector, scalar) type.
apsimPublish(name, type, structure)
Publish an apsim event called “name”. For non-null events, a “type” structure is sent.
apsimSubscribe(name, type, handler)
Subscribe to an apsim event called “name”. When the event occurs, the R function called “handler” is invoked with a structure of the nominated type as its argument.
Exposes an R variable called “name” to the apsim system.
Query the system for:
“*” will return a list of all components.
“wheat.*” will return a list of all variables for the wheat module
“*.lai” will return a list of all lai variables for all modules.
Cause a fatal error in the simulation.
Sow, harvest and end a wheat crop.
apsimPublish("wheat.sow", "Sow", list(plants=100, sowing_depth=25, Cultivar="hartog")) ... apsimPublish("wheat.harvest", "Harvest", list(Remove=1.0)) apsimPublish("wheat.end_crop", "Null", list())
apsimPublish("fertiliser.apply", "FertiliserApplication", list(Amount = 100, Depth = 50, Type = "NO3_N"))