How can I call a .C function compiled with Matlab Coder in R?

167 Views Asked by At

I would like to call a C function compiled using Matlab Coder in R for later use in a Shiny app. How can I go about doing this. The top of the .C function looks like this:

double ensembleBaggedTreesExtendedStay_V1(double TransferredYesNo,
                                          double BleedDisYesNo,
                                          double PreOpAlbumin, double PreOpHCT,
                                          double ASAClass, double ComplexYesNo)

and the file name for the c function is "ensembleBaggedTreesExtendedStay_V1.c"

I have very limited knowledge of R and .C and I would appreciate any help.

Thanks!

1

There are 1 best solutions below

0
Ryan Livingston On

When you'd like to compile MATLAB Coder generated code on your own or pull it into an IDE, you can package your code and all of its compile-time and run-time dependencies using Coder. For example:

codegen yourFunction -args -config:lib
packNGo('codegen/lib/yourFunction')

That produces yourFunction.zip containing generated sources, dependent headers/sources, and any library dependencies.

The documentation link above shows how to do this in the Coder App.

Once you have that ZIP file, you can unzip it and use the command noted in the comments by @tpetzoldt R CMD SHLIB along with the extension system to compile all of the C files present in that ZIP file.