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

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.

回答 (1 件)

Harsh
Harsh 2025 年 1 月 2 日

0 投票

Hi Arsalan,
Proceed with the steps outlined below to call the C function generated by MATLAB coder in R using .C( )
  • Modify your ensembleBaggedTreesExtendedStay_V1 function such that it returns void and stores the desired result in one or more arguments expressly created for this purpose. Also, the function should have a C_ prefix.
  • Compile the function into a shared library accessible to R using this command:
R CMD SHLIB ensembleBaggedTreesExtendedStay_V1.c
  • Within an active R session, link to the shared library using dyn.load("ensembleBaggedTreesExtendedStay_V1.so").
  • Write an R wrapper to call the C function using .C with the following syntax: .C("C_my_func", arg1, arg2).
Hope this resolves your query. Happy coding !

カテゴリ

ヘルプ センター および File ExchangeMATLAB Coder についてさらに検索

製品

タグ

質問済み:

2023 年 8 月 29 日

回答済み:

2025 年 1 月 2 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by