1

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!

7
  • 2
    Difficult to give a precise answer without knowing the exact C libraries needed by Matlab C Coder for this function. However perhaps this gives you a start point : r-bloggers.com/2021/07/using-r-calling-c-code-with-rcpp Commented Apr 8, 2023 at 7:46
  • 1
    You need to compile the function with the Gnu C compiler at the command line with R CMD SHLIB file.c and then call it from R with .C. More about this can be found in the Writing R Extensions manual. Commented Apr 8, 2023 at 7:47
  • Thank you for your help. I tried R CMD SHLIB file.c but it resulted in the following error: clang -arch arm64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c ensembleBaggedTreesExtendedStay_V1.c -o ensembleBaggedTreesExtendedStay_V1.o In file included from ensembleBaggedTreesExtendedStay_V1.c:13: In file included from ./ensembleBaggedTreesExtendedStay_V1.h:16: ./rtwtypes.h:42:10: fatal error: 'tmwtypes.h' file not found #include "tmwtypes.h" Commented Apr 8, 2023 at 8:27
  • 1
    As anticipated, tmwtypes.h is one of the Matlab libraries you'll need to include, see fr.mathworks.com/help/coder/ug/… Commented Apr 8, 2023 at 10:24
  • 1
    It will not be easy to compile this file. It has lots of dependencies. Start by reading the documentation to MATLAB Coder. Commented Apr 8, 2023 at 13:33

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.