# Executing OSM from R

The ‘system’ function in R can be used to execute OSM in the same way as executing OSM from a Windows DOS batch file. R can also be used to develop the OSM command file and/or load and post-process the projected tree list report CSV file generated during simulation to produce simulation graphics or other statistics.

system("C:/OSM/Program/OSM.ConsoleApp.exe Acadian C:/OSM/Demos/MyOsmCommandFile.osmc")

# Linking OSM Libraries directly into R

The simple method above is recommended 99% of the time to run OSM in R, but for those really wanting to link directly into the OSM API from R, you can do this, but it is not straightforward.

It is possible to initialize OSM Variant objects from R and submit OSM commands.

It is also possible to work directly with OSM stand objects and tree objects in R, but this has not been tested yet.

### Install the Common Language Runtime package (rClr) for R

For R to be able to load OSM DLLs and communicate with OSM objects, a special Common Language Runtime (CLR) package for R must be downloaded and installed in R:

1. Download the R Common Language Runtime (rClr) Wrapper Package.

2. Install this package from the R console (requires R 3.0.1 or higher).

3. You may also need to install a MS C++ for Visual Studio update from: http://www.microsoft.com/en-us/download/confirmation.aspx?id=30679

4. Load the rClr package from the R console.

5. Try to run this method from R:

clrCallStatic("Rclr.HelloWorld", "Hello")

Assuming steps 1-5 have been completed, you should now be able to use OSM.

### Loading OSM DLLs and objects

Load the main OSM Command Runtime Assembly (OSM.Management.dll).

clrLoadAssembly("C:/.../OSM. Management.dll")

Using the static ‘Load” method from the OSM.Runtime type, create a new OSM Runtime object that references your OSM variant DLL (argument #2) and the OSM variant simulation type (argument #3), where ‘Variant’ is your Variant’s name.

osm <-clrCallStatic('OSM.Runtime', "Load", "C:/…/OSM.Variants.Variant.dll","OSM.Variants.Variant.VariantSimulation")

OSM.Runtime.Load example using the Acadian variant model:

osm <-clrCallStatic('OSM.Runtime', "Load", "C:/…/OSM.Variants.Acadian.dll","OSM.Variants.Acadian.AcadianSimulation")

Assuming the Variant developer respected OSM DLL and OSM type naming conventions, this shorthand argument version of the ‘Load’ method can be used instead. OSM internally constructs the appropriate DLL path and simulation type name as described above.

osm <-clrCallStatic('OSM.Runtime', "Load", "Variant")

### Submitting commands via OSM.Runtime

Important Note: Use the 32-bit version of R if the OSM tree record input data source uses 32 bit database connection drivers. Microsoft Office products (Access, Excel) use 32-bit database connection drivers.

Using the OSM runtime object you initialized above, you can submit OSM commands from a text file using the method RunCommandFile:

clrCall(osm,'RunCommands','MyOsmCommands.txt')

Or by submitting a commands one-by-one using the method RunCommands:

clrCall(osm,'RunCommands','INPUTS.SOURCE C:/…/MyTreeList.mdb')

clrCall(acadian,'RunCommands','OUTPUTS.StandSummary.ConsoleOn True')

clrCall(osm,'RunCommands','OUTPUTS.TreeList.FilePath C:/…/MyProjectedTreeList.txt')

clrCall(osm,'RunCommands','SIMULATE SurveyID')

### Working with OSM tree list results in R

While it is possible to work with all OSM stand and tree objects via the rClr, it is probably easier (and faster) to read tree list projection results from a comma separated text file output from OSM into R.

treeList <- read.csv("C:/…/MyProjectedTreeList.txt", quote="")

These methods described above could easily be simplified into an R script or function to allow one or more stands to be simulated and graphed with one R command.

For detailed information on OSM objects see OSM source code documentation.